【发布时间】:2015-03-07 07:35:29
【问题描述】:
我有以下代码
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#chatresults').load('includes/chat.php');
}, 3000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
});
效果很好。这会每 3000 毫秒加载一次页面,但它会永远加载一次,这意味着如果有人让他们的浏览器保持打开状态,我的服务器存储空间就会被大量使用,因为它会在另一个页面上运行 MySQL 查询。
如何限制它,使其每 3000 秒调用一次,但在 10 分钟后(或 X 加载后)停止(直到页面刷新/更改)?
我正在尝试其中一个答案,但它不起作用。我是 AJAX 的菜鸟,我做得对吗?
if (counter == 12) {
clearInterval(aux);
}
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
aux = setInterval(function() {
$('#chatresults').load('includes/chat.php');
}, 3000);
counter++;
});
【问题讨论】:
-
每次循环时,将所用时间相加,直到等于 10 分钟?然后,您可以将其关闭。