1

I am working on an application that sends current timestamp to database every 2 minutes with AJAX using setInterval.

But somehow setInterval stops after some minutes (i didnt calculate the exact time), but i believe it happens when i dont open that browser's tab for 20-30 minutes.

function tmstmp() {
$.post("send_functions.php?act=time");
}

$(function() { 
setInterval(tmstmp, 60000);
});

Is that normal that setInterval stops if that tab is not on foreground ?

If yes, how can i prevent setInterval to stop ? or check if it stopped ?

Thanks

Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130
  • 3
    See this question: http://stackoverflow.com/questions/6951727/setinterval-not-working-properly-on-chrome –  Apr 15 '12 at 20:21
  • Check my edited answer. Should at least give you a clue if the interval works. –  Apr 15 '12 at 20:27
  • @amnotiam You can't make comments wiki!!! – gdoron Apr 15 '12 at 20:39
  • BTW, why would you want to send timestamps to your web server? I am sure that there is a way for your web server to acquire the exact timestamp data you are trying to send it with every POST request. –  Apr 15 '12 at 20:43
  • i am sending it to mysql to "last_access" row to check if user still online. – Utku Dalmaz Apr 15 '12 at 20:44
  • @gdoron: Very true. Of course there's no rep for comments, so I'm fine with that... for now. –  Apr 15 '12 at 20:44
  • 1
    @amnotiam. You gave me an idea for a new question in `Meta` LOL... – gdoron Apr 15 '12 at 20:47
  • This might be a more useful example to work with (with how I would do it with `setTimeout()` instead): http://jsfiddle.net/kxa53/ – Jared Farrish Apr 15 '12 at 21:07

3 Answers3

1

You should try to make an function call on page startup:

test();

and then loop that function:

function test() {
    setTimeout(function() {
        // your code
        test();
    }, 2000);
}
jaycer
  • 2,941
  • 2
  • 26
  • 36
Jakub
  • 11
  • 1
0

No code + no debug information = hard to tell what went wrong.

Just to be sure add the following line to the code (method) that gets executed with setInterval and watch after 20-30 minutes if you still get output in the console.

console.log('Yep! I am alive!');

EDIT: Could be anything but try changing the tmstmp method to include a callback function after the POST request gets executed. That way you'll at least know that it works.

function tmstmp() {
    $.post("send_functions.php?act=time", function(data){
        console.log('Yep! I am alive!');
    });
}
  • Well... If you had the code, would you spend the time debugging it- meaning wait couple of times 20-30 minutes? **:)** – gdoron Apr 15 '12 at 20:21
  • @gdoron just change the interval time to a few seconds while you're debugging. – jbabey Apr 15 '12 at 20:31
  • will try and write here thx. by the way should i use setTimeout with repeated function ? is it better than setInterval ? – Utku Dalmaz Apr 15 '12 at 20:33
  • Generally `setTimeout()` has proven to be much more reliable than `setInterval()`. –  Apr 15 '12 at 20:37
  • 1
    @holodoc - I prefer `setTimeout()` to `setInterval()` in many cases where they may both work, but with a 60 second interval, I don't know the statement that one is more or less reliable is accurate. – Jared Farrish Apr 15 '12 at 21:21
0

That's not supposed to happen. Browsers may indeed reduce the timer resolution to something around 1/s, but not clear the setInterval.

Could there be some bug in your code that causes a clearInterval?

user123444555621
  • 148,182
  • 27
  • 114
  • 126