我使用 jQuery ajax 每 2 秒刷新一个 div。
如何在 60 秒后停止此刷新(例如,如果用户处于非事件状态)?
setInterval(function() {
$.ajax({
url: "feeds.php",
cache: false
}).done(function( html ) {
$('#feeds').replaceWith('<div id="feeds">'+html+'</div>');
});
}, 2000);
谢谢!
最佳答案
将 setInterval 句柄分配给一个变量,您将使用该句柄在 60 秒后清除它。
var interval = setInterval(function(){ ... }, 2000);
// start a 60 second timer
setTimeout(function(){ window.clearInterval(interval); }, 60000);
关于jquery - X 秒后停止 jQuery ajax 刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11493590/