每隔X分钟用AJAX调用一个文件,直到Y时间?

时间:2022-12-17 01:08:05

I have the following code

我有以下代码

$(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.
});

It works beautifully. This loads the page every 3000 milliseconds, but it does it forever, meaning that if someone leaves their browser open, my server storage gets used up a lot since it runs a MySQL query on that other page.

它的工作原理非常漂亮。它每3000毫秒加载一次页面,但它会一直加载,这意味着如果有人打开浏览器,我的服务器存储会被占用很多,因为它在另一个页面上运行MySQL查询。

How can I limit this so it calls it every 3000 seconds, but after 10 minutes (or after X loads) it stops (until page is refreshed/changed)?

我如何限制它,所以它每3000秒调用一次,但是10分钟后(或者在X加载之后)它停止(直到页面刷新/更改)?

I'm trying one of the answers, but it's not working. I'm a noob when it comes to AJAX, am I doing it correctly?

我正在尝试其中一个答案,但行不通。说到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++;

    });

2 个解决方案

#1


2  

Three easy steps:

三个简单的步骤:

  1. Assign setInterval to a handler:

    为处理程序分配setInterval:

    auxHandler = setInterval(function() {
        $('#chatresults').load('includes/chat.php');
    }, 3000);
    
  2. Keep track of the number of times that the ajax is called with a variable (e.g.: call it counter)

    跟踪用变量调用ajax的次数(例如:调用计数器)

    counter++;
    
  3. When counter reaches the maximum number of calls, clear the interval:

    当计数器达到最大调用次数时,清除间隔:

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

In your particular case, the code would look like this:

在你的例子中,代码是这样的:

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); 
});

You can also see it working on this jsfiddle: http://jsfiddle.net/utrdha8f/1/ (changing your chat call for a console.log)

您还可以看到它在这个jsfiddle运行:http://jsfiddle.net/utrdha8f/1/(更改console.log的聊天调用)

#2


1  

I think what you are looking for is in this answer

我认为你要找的是这个答案

refresh at given times not on an interval.

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

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);
}

Then you can add a script tag to call the refreshAt() function.

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

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

#1


2  

Three easy steps:

三个简单的步骤:

  1. Assign setInterval to a handler:

    为处理程序分配setInterval:

    auxHandler = setInterval(function() {
        $('#chatresults').load('includes/chat.php');
    }, 3000);
    
  2. Keep track of the number of times that the ajax is called with a variable (e.g.: call it counter)

    跟踪用变量调用ajax的次数(例如:调用计数器)

    counter++;
    
  3. When counter reaches the maximum number of calls, clear the interval:

    当计数器达到最大调用次数时,清除间隔:

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

In your particular case, the code would look like this:

在你的例子中,代码是这样的:

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); 
});

You can also see it working on this jsfiddle: http://jsfiddle.net/utrdha8f/1/ (changing your chat call for a console.log)

您还可以看到它在这个jsfiddle运行:http://jsfiddle.net/utrdha8f/1/(更改console.log的聊天调用)

#2


1  

I think what you are looking for is in this answer

我认为你要找的是这个答案

refresh at given times not on an interval.

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

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);
}

Then you can add a script tag to call the refreshAt() function.

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

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