【问题标题】:Call a file with AJAX every X minutes until Y time?每 X 分钟使用 AJAX 调用一个文件,直到 Y 时间?
【发布时间】: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 分钟?然后,您可以将其关闭。

标签: jquery ajax


【解决方案1】:

三个简单的步骤:

  1. 将 setInterval 分配给处理程序:

    auxHandler = setInterval(function() {
        $('#chatresults').load('includes/chat.php');
    }, 3000);
    
  2. 跟踪使用变量调用 ajax 的次数(例如:调用它counter

    counter++;
    
  3. counter达到最大调用次数时,清除间隔:

    if (counter == MAX_NUM_CALLS) {
        clearInterval(auxHandler);
    }
    

在您的特定情况下,代码如下所示:

var intervalHandler;
var counter = 0;

$(document).ready(function() {
    $.ajaxSetup({ cache: false });
    intervalHandler = setInterval(function() {
        $('#chatresults').load('includes/chat.php');
        counter++;
        if (counter == 12) {
            clearInterval(intervalHandler);
        }
    }, 3000); 
});

您还可以在此 jsfiddle 上看到它:http://jsfiddle.net/utrdha8f/1/(将您的聊天呼叫更改为 console.log)

【讨论】:

  • 此解决方案完美运行,除非用户弄乱了您的 JavaScript 代码。第二个(更安全)的选择:打开一个 PHP 会话,跟踪来自该用户会话的调用次数,当请求达到某个数量时忽略请求
  • 我试过这个,但它似乎没有做任何事情。我将用我的代码更新 OP,我这样做对吗?
  • 它不起作用 - 仍然持续加载并且不会停止。 :( aux 是否设置为 clearInterval 以外的任何值?这有关系吗?
  • 我的错:我更改了变量的名称以使其更具可读性,但忘记替换其中一个。您现在可以在小提琴中看到它正在工作:jsfiddle.net/utrdha8f/1(请注意 console.log 在 12 之后如何停止)。如果您发现任何问题,请告诉我。
【解决方案2】:

我想你要找的就是这个answer

在给定时间刷新,而不是按时间间隔刷新。

function refreshAt(hours, minutes, seconds) {
    var now = new Date();
    var then = new Date();

    if(now.getHours() > hours ||
       (now.getHours() == hours && now.getMinutes() > minutes) ||
        now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
        then.setDate(now.getDate() + 1);
    }
    then.setHours(hours);
    then.setMinutes(minutes);
    then.setSeconds(seconds);

    var timeout = (then.getTime() - now.getTime());
    setTimeout(function() { window.location.reload(true); }, timeout);
}

然后你可以添加一个脚本标签来调用refreshAt()函数。

refreshAt(15,35,0); //Will refresh the page at 3:35pm

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多