我试图对进入AJAX结果div
的一些数据每7秒进行一次AJAX刷新。
我需要控制它何时发生,因为我有许多事件,如文本编辑和可拖动的东西通过jQueryUI完成。我设置了一个带有条件和if
语句的函数来设置间隔或根据条件清除它。
但我的功能似乎认识到一个真实的状态,但不是一个错误的状态。任何想法或是他们更好的方式来实现我想要的?非常感谢你的时间!
var intervalStart = true;
function setupAjaxInterval(condition){
if (condition == true) {
var refreshContent = setInterval(function() {
var datastring = 'refreshpage=true&projid=' + proj_id + '&uid=' + uid;
ajaxUploadData(datastring);
alert('pagerefresh');
}, 7000);
}
else if (condition == false) {
clearInterval(refreshContent);
alert('nofresh');
}
}
setupAjaxInterval(intervalStart);
//works for true but not false.
答案 0 :(得分:2)
你在这里处理范围。
您在if语句中声明var refreshCount
,因此它不再存在于else中。
var refreshContent;
function setupAjaxInterval(condition){
if (condition == true) {
refreshContent = setInterval(function() {
var datastring = 'refreshpage=true&projid=' + proj_id + '&uid=' + uid;
ajaxUploadData(datastring);
alert('pagerefresh');
}, 7000);
}
else if (condition == false) {
clearInterval(refreshContent);
alert('nofresh');
}
}
答案 1 :(得分:1)
您的问题是您在函数内声明了refreshContent
。一旦函数返回,你就会丢失引用,所以当你稍后用false调用它时,不再定义refreshContent
。
要解决此问题,请将var refreshContent;
放在功能之外。
编辑:要么删除它,要么删除它前面的var
并使其成为全局。
答案 2 :(得分:0)
refreshContent
未定义,如果
var refreshContent;
function setupAjaxInterval(condition){
答案 3 :(得分:-1)
refreshContent被设置为
范围内的局部变量 if( condidtion == true )
所以当你进入
else if( condition == false )
不再定义refreshContent。
您需要将refreshContent定义为函数的范围