checkForChangesSupport

checkForChangesSupport

尽管下面每隔5000ms在我的代码中就有supportchatinterval = null;clearInterval(supportchatinterval);(请参见onBlur()onFocus()),该函数仍以相同的setInterval(请参见checkForChangesSupport())加载getTableSupport.php。要停止setInterval,直到再次调用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>

最佳答案

没有间隔需要清除,因为每次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>

09-27 13:38