1

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.

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)?

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?

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++;

    });
Drennon
  • 15
  • 4
  • 2
    How about every time you loop, you add the time taken until it equals 10 minutes? Then, you can shut it down. – SirPython Jan 09 '15 at 02:13

2 Answers2

2

Three easy steps:

  1. Assign setInterval to a handler:

    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)

    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)

Alvaro Montoro
  • 28,081
  • 7
  • 57
  • 86
  • This solution works perfectly unless the user messes with your JavaScript code. A second (and safer) option: open a PHP session, keep track of the number of calls from that user's session, ignore the requests when they reach some number – Alvaro Montoro Jan 09 '15 at 02:20
  • I've tried this, but it doesn't seem to do anything. I'll update OP with my code, am I doing it correct? – Drennon Jan 09 '15 at 04:15
  • It does not work - still loads continuously and does not stop. :( Is aux set to anything other than in the clearInterval? Does that matter? – Drennon Jan 09 '15 at 05:05
  • My bad: I changed the name of the variables to make it more readable and forgot to replace one of them. You can see it working now in the fiddle: http://jsfiddle.net/utrdha8f/1/ (notice how the console.log stops after 12). Please let me know if you find any issue. – Alvaro Montoro Jan 09 '15 at 05:11
0

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(15,35,0); //Will refresh the page at 3:35pm
Community
  • 1
  • 1
gmaniac
  • 940
  • 1
  • 17
  • 33