2015-08-22 31 views
0

我正在構建一個web應用程序來幫助一家餐廳,而且我的客戶非常清楚他希望應用程序是異步的。setInterval()沒有按時停止,間隔「跳躍」

在本地主機的發展我用setInterval來逐步periodicaly更新頁面使用此代碼的變化爲每個按鈕:

$('#showWaitlist').click(function(){ 
    showWaitlist(); 
    stopUpdate(interval); 
    interval = setInterval(function() { showWaitlist();}, intervalTime); 
}); 

function stopUpdate(){ 
    clearInterval(interval); 
} 

所以,每次我點擊一個按鈕時停止過去的時間間隔,並啓動它自己。 但是,當我將應用程序移動到服務器時,它的工作正常,有時候這些事件有時會相互重疊,特別是當服務器的負載較重時或者我使用手機訪問網站時。

如果setInterval和stopUpdate是js,他們不應該在客戶端工作,並且幾乎是即時的?什麼可能導致這個間隔之間的「跳躍」? 這是一個鏈接到網站:http://www.emc2.mx/Pruebas/unicenta/PostodoroApp/

請注意,問題並不總是會發生,但你可以複製它,如果你在手機上運行它。

我會在這裏添加showWaitlist,但我懷疑它有什麼問題。

function showWaitlist(){ 
    $.ajax({     
     type:'GET', 
     url: 'waitlist.php', 
     dataType: 'html', 
     success: function(result){ 
      $('#result_table').html(result); 
     } // End of success function of ajax form 
    }); // End of ajax call 
} 
+0

st代碼是什麼opUpdate()? –

+0

感謝您指出!我忘了它不是香草js。我更新了帖子 – TianRB

回答

1

您將需要通過添加以下行

clearInterval(interval); // stop the timer once the time finishes. 

以下行

$('#result_table').html(result); 

你需要後清除間隔 您的代碼Ajax調用成功在成功方法回調中訪問區間變量

+0

這不會阻止它更新嗎?我只想停止更新,如果我點擊一個不同的按鈕。如果我清楚Ajax成功,它會導致只更新一次不會嗎? – TianRB

+0

所以你必須捕捉身體click事件和 $( '身體')。點擊(函數(EVT){ 如果(evt.target.id!== 「showWaitlist」){// 調用clearInterval } } –