Javascript clearInterval和将变量设置为null不起作用

Javascript clearInterval和将变量设置为null不起作用,javascript,jquery,Javascript,Jquery,尽管有supportchatinterval=null和清除间隔(supportchatinterval)在我下面的代码中(请参见onBlur()和onFocus()),每5000ms该函数仍以相同的设置间隔加载getTableSupport.php(请参见checkForChangesSupport()。设置onBlur函数时,我希望停止设置间隔,直到再次调用onFocus <script> supportchatinterval = 5000; $(document).rea

尽管有
supportchatinterval=null
清除间隔(supportchatinterval)在我下面的代码中(请参见
onBlur()
onFocus()
),每5000ms该函数仍以相同的设置间隔加载getTableSupport.php(请参见
checkForChangesSupport()
。设置onBlur函数时,我希望停止设置间隔,直到再次调用
onFocus

<script>

supportchatinterval = 5000;

$(document).ready(function(){
    checkForChangesSupport();
    setTimeout(checkForChangesSupport, supportchatinterval);
});

function checkForChangesSupport() {
   $.get("getCountSupport.php", function(response) {
      if(response == 1) {
         refreshTableSupport();
      }
setTimeout(checkForChangesSupport, supportchatinterval)
   });
}

function refreshTableSupport(){
    $('#tableHolderSupport').load('getTableSupport.php');
}

</script>

<script type="text/javascript">

function onBlur(){
document.body.className = 'blurred';
$.get("afk.php?afk=1");
supportchatinterval = null;
clearInterval(supportchatinterval);
};

function onFocus() {
document.body.className = 'focused';
$.get("afk.php?afk=0");
    supportchatinterval = 5000;
refreshTableSupport();
}

</script>

支持间隔=5000;
$(文档).ready(函数(){
检查ChangesSupport();
设置超时(检查更改支持、支持聊天间隔);
});
函数checkForChangesSupport(){
$.get(“getCountSupport.php”,函数(响应){
如果(响应==1){
refreshTableSupport();
}
设置超时(检查ChangesSupport、supportchatinterval)
});
}
函数refreshTableSupport(){
$('#tableHolderSupport').load('getTableSupport.php');
}
函数onBlur(){
document.body.className=‘模糊’;
$.get(“afk.php?afk=1”);
supportchatinterval=null;
clearInterval(supportchatinterval);
};
函数onFocus(){
document.body.className='focused';
$.get(“afk.php?afk=0”);
支持间隔=5000;
refreshTableSupport();
}

没有要清除的间隔,因为每次运行
checkForChangesSupport()
时都会创建新的超时。无论哪种方式,
supportchatinterval
都只是一个整数,不能“清除”

要停止此操作,您可以引入一个标志并检查函数是否应该运行。此外,您应该调用
checkForChangesSupport()
再次启动计时器

<script>

supportchatinterval = 5000;
var flag = 1;

$(document).ready(function(){
    checkForChangesSupport();
    setTimeout(checkForChangesSupport, supportchatinterval);
});

function checkForChangesSupport() {
   if(flag){
      $.get("getCountSupport.php", function(response) {
         if(response == 1) {
            refreshTableSupport();
         }
         setTimeout(checkForChangesSupport, supportchatinterval)
      });
   }
}

function refreshTableSupport(){
    $('#tableHolderSupport').load('getTableSupport.php');
}

</script>

<script type="text/javascript">

function onBlur(){
document.body.className = 'blurred';
$.get("afk.php?afk=1");
flag = 0;
};

function onFocus() {
document.body.className = 'focused';
$.get("afk.php?afk=0");
flag = 1;
checkForChangesSupport();
}

</script>

支持间隔=5000;
var标志=1;
$(文档).ready(函数(){
检查ChangesSupport();
设置超时(检查更改支持、支持聊天间隔);
});
函数checkForChangesSupport(){
国际单项体育联合会(旗){
$.get(“getCountSupport.php”,函数(响应){
如果(响应==1){
refreshTableSupport();
}
设置超时(检查ChangesSupport、supportchatinterval)
});
}
}
函数refreshTableSupport(){
$('#tableHolderSupport').load('getTableSupport.php');
}
函数onBlur(){
document.body.className=‘模糊’;
$.get(“afk.php?afk=1”);
flag=0;
};
函数onFocus(){
document.body.className='focused';
$.get(“afk.php?afk=0”);
flag=1;
检查ChangesSupport();
}

没有要清除的间隔,因为每次运行
checkForChangesSupport()
时都会创建新的超时。无论哪种方式,
supportchatinterval
都只是一个整数,不能“清除”

要停止此操作,您可以引入一个标志并检查函数是否应该运行。此外,您应该调用
checkForChangesSupport()
再次启动计时器

<script>

supportchatinterval = 5000;
var flag = 1;

$(document).ready(function(){
    checkForChangesSupport();
    setTimeout(checkForChangesSupport, supportchatinterval);
});

function checkForChangesSupport() {
   if(flag){
      $.get("getCountSupport.php", function(response) {
         if(response == 1) {
            refreshTableSupport();
         }
         setTimeout(checkForChangesSupport, supportchatinterval)
      });
   }
}

function refreshTableSupport(){
    $('#tableHolderSupport').load('getTableSupport.php');
}

</script>

<script type="text/javascript">

function onBlur(){
document.body.className = 'blurred';
$.get("afk.php?afk=1");
flag = 0;
};

function onFocus() {
document.body.className = 'focused';
$.get("afk.php?afk=0");
flag = 1;
checkForChangesSupport();
}

</script>

支持间隔=5000;
var标志=1;
$(文档).ready(函数(){
检查ChangesSupport();
设置超时(检查更改支持、支持聊天间隔);
});
函数checkForChangesSupport(){
国际单项体育联合会(旗){
$.get(“getCountSupport.php”,函数(响应){
如果(响应==1){
refreshTableSupport();
}
设置超时(检查ChangesSupport、supportchatinterval)
});
}
}
函数refreshTableSupport(){
$('#tableHolderSupport').load('getTableSupport.php');
}
函数onBlur(){
document.body.className=‘模糊’;
$.get(“afk.php?afk=1”);
flag=0;
};
函数onFocus(){
document.body.className='focused';
$.get(“afk.php?afk=0”);
flag=1;
检查ChangesSupport();
}

在您的情况下,supportchatinterval
与当前正在运行的超时无关。它只是代码运行的时间。您需要做的是存储生成的实际超时id

theTimeout = setTimeout(checkForChangesSupport, supportchatinterval);
你用这个

window.clearTimeout(theTimeout);

supportchatinterval
在您的情况下,与当前正在运行的超时无关。它只是代码运行的时间。您需要做的是存储生成的实际超时id

theTimeout = setTimeout(checkForChangesSupport, supportchatinterval);
你用这个

window.clearTimeout(theTimeout);

我建议您使用
setInterval
,它更适合您的用例

然后,在“模糊”上可以清除它

您的主要问题是混淆了间隔的延迟(作为参数提供)和设置间隔时返回的句柄,然后将其用作参数os
clearInterval
来取消间隔

代码:

如果不仅要停止在模糊上调用间隔函数,而且要在出现结果时停止调用间隔函数,只需在此处调用
clearInterval

    function checkForChangesSupport() {
        $.get("getCountSupport.php", function(response) {
            if (response == 1) {
               refreshTableSupport();
               clearInterval(intervalid);
            }
        });
    }

我建议您使用
setInterval
,它更适合您的用例

然后,在“模糊”上可以清除它

您的主要问题是混淆了间隔的延迟(作为参数提供)和设置间隔时返回的句柄,然后将其用作参数os
clearInterval
来取消间隔

代码:

如果不仅要停止在模糊上调用间隔函数,而且要在出现结果时停止调用间隔函数,只需在此处调用
clearInterval

    function checkForChangesSupport() {
        $.get("getCountSupport.php", function(response) {
            if (response == 1) {
               refreshTableSupport();
               clearInterval(intervalid);
            }
        });
    }

1) 您正在混合
setTimeout
setInterval
。2)
setTimeout
返回超时id。感谢我的回答,PHP与此问题无关,请删除标记:)为什么要将答案编辑到问题中?请不要这样做,这很混乱。请不要在你的问题中包含我答案中的代码。你用这种方式使所有答案无效。1)你混合了
setTimeout
setInterval
。2)
setTimeout
返回超时id。感谢我的回答,PHP与此问题无关,请删除标记:)为什么要将答案编辑到问题中?请不要这样做,这很混乱。请不要在你的问题中包含我答案中的代码。你用这种方式否定了所有的答案。这让我很尴尬,让我来试一试,看看关于clearInterval的文档。我将