2016-07-25 133 views
0

儘管有下面我的代碼supportchatinterval = null;clearInterval(supportchatinterval);(見onBlur()onFocus())每5000毫秒的功能仍然是在同一的setInterval加載getTableSupport.php(見checkForChangesSupport()。當該功能的onblur設置我想停止的setInterval直到onFocus再次調用。clearInterval和設置變量爲null不工作

<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> 
+0

1)你混合'setTimeout'和'setInterval'。 2)'setTimeout'返回超時ID。 – gcampbell

+0

謝謝我顯然是瞎的 – michelle

+0

PHP與這個問題無關,請刪除標籤:) –

回答

1

有沒有間隔清除,因爲新的超時每次創建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> 
+0

這讓我很尷尬,讓我試試看,並閱讀有關clearInterval的文檔。如果一切順利,我會接受。謝謝。抱歉。 – michelle

+0

有人指出,我在上面的函數中使用setTimeout應該將其更改爲setInterval? – michelle

+0

更好地翻轉你的最後兩條語句。否則,你調用'clearInterval(null);' – aquinas

1

supportchatinterval你的情況與正在運行的超時沒有任何關係。所有的代碼運行需要多長時間。你需要做的是存儲生成的實際超時ID。

theTimeout = setTimeout(checkForChangesSupport, supportchatinterval); 

和您使用

window.clearTimeout(theTimeout); 
0

我建議你使用setInterval,這是比較足夠你的使用情況。

然後,模糊你可以清除它。

你的主要問題是,當它被設置,然後作爲參數OS clearInterval取消它被返回的時間間隔(作爲參數給出)及其手柄延遲之間的混淆。

代碼:

// This is a delay between function calls, in milliseconds 
    supportchatinterval = 5000; 
    // This will be the handle returned by setInterval, which be usefull to cancel 
    // it later 
    intervalid = null; 

    $(document).ready(function(){ 
     checkForChangesSupport(); 
     intervalid = setInterval(checkForChangesSupport, supportchatinterval); 
    }); 

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

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

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

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

如果不僅要停止呼籲模糊區間的功能,而且當結果都存在,只是調用clearInterval有:

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