我正在构建一个网络应用程序来帮助一家餐厅,我的客户非常清楚他希望该应用程序是异步的。
在本地主机开发期间,我使用 setInterval 定期更新页面,并为每个按钮使用此代码的变体:
$('#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
}
最佳答案
您需要清除以下时间间隔 通过添加以下行即可成功对代码进行 ajax 调用
clearInterval(interval); // stop the timer once the time finishes.
在以下行之后
$('#result_table').html(result);
您需要在成功方法回调中访问间隔变量
关于javascript - setInterval() 未按时停止,间隔之间为 "jumping",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160445/