19

a question. If i use setInterval in this manner:

setInterval('doSome();',60000);

am i safe that the doSome() function is triggered every 60 seconds, even if I change the tab in a browser?

3
  • 7
    You should never pass a string to setInterval but a function: setInterval(doSome, 60000); or if you need arguments etc: setInterval(function() { doSome(12345); }, 60000); Apr 27, 2011 at 8:58
  • of course. i suggest you to type it as setInterval(doSome, 60000) instead, which is more efficient.
    – gpilotino
    Apr 27, 2011 at 8:59
  • Strings passed to setInterval are fine. The difference is how they are evaluated and from what scope. Apr 27, 2011 at 9:13

8 Answers 8

33

Passing a string to setInterval is fine, and is one of two ways to use setInterval, the other is passing a function pointer. It is not wrong in any way like the other answers state, but it is not as efficient (as the code must be reparsed) nor is it necessary for your purpose. Both

setInterval('doSome();', 60000); // this runs doSome from the global scope
                                 // in the global scope

and

setInterval(doSome, 60000);      // this runs doSome from the local scope
                                 // in the global scope

are correct, though they have a slightly different meaning. If doSome is local to some non-global scope, calling the latter from within the same scope will run the local doSome at 60000ms intervals. Calling the former code will always look for doSome in the global scope, and will fail if there is no doSome function in the global scope.

The function will reliably be triggered, regardless of tab focus, at intervals of at least 60000ms, but usually slightly more due to overheads and delays.

All browsers clamp the interval value to at least a certain value to avoid intervals being too frequent (I think it's a minimum of 10ms or 4ms or something, I can't exactly remember).

Note that some browsers (the upcoming Firefox 5 is one, but there are probably others that I don't know of) further clamp setInterval drastically to e.g. 1000ms if the tab is not focused. (Reference)

1
  • One more difference - in the first version, you can change the value of the global variable doSome and the interval will start calling the new function. In the second version you can't do that.
    – Vilx-
    Dec 15, 2011 at 9:31
2

No, the interval cannot execute until the event loop is cleared, so if you do for instance setInterval(func, 1000); for(;;) then the interval will never run. If other browsers tabs run in the same thread (as they do everywhere(?) except for in chrome, then the same applies if those tabs clog the event loop.)

But for an interval as large as 60000 it is at least very likely that the func will be called in reasonable time. But no guarantees.

1

If the tab with the setInterval() function remains open, then yes the function will be executed every 60 seconds, even if you switch to or open other tabs.

0

Yeah it works on an example I just created.

http://jsfiddle.net/5BAkx/

0

Yes, the browser's focus is irrelevant.

However, you should not use a string argument to setInterval. Use a reference to the function instead:

setInterval(doSome, 60000);
3
  • Strings passed to setInterval are fine. The difference is how they are evaluated and from what scope. Apr 27, 2011 at 9:12
  • @Delan And the speed, and syntax highlighting, and the need to do stupid things like escaping quotation marks. Passing strings to setInterval is not wrong, per se, just brainless. Apr 27, 2011 at 10:30
  • That is true, and I agree with you. Apr 27, 2011 at 10:41
0

No, you are not guaranteed exact time safety. JS is event based (and single-threeaded) so the event won't fire at the exact right moment, especially not if you have other code running at the same time on your page.

The event will fire in the neighbourhood of the set time value, but not on the exact millisecond. The error may be tens of milliseconds even if no other event is running at the time. This may be an issue if for example you have a long-running process where the timing is important. If you do, you'll need to synchronize with a clock once in a while.

0

Yes it will be called as long as the page is open, regardless the tab is switched or even the browser is minimized.

However make sure you pass the function not a string to setInterval

it should be >

setInterval(doSome, 60000)

1
  • Strings passed to setInterval are fine. The difference is how they are evaluated and from what scope. Apr 27, 2011 at 9:13
0

About "exact time safety": The following code starts UpdateAll at intervals of RefreshInterval milliseconds, with adjustment each second so that one start occurs at each second at the start of the second. There will be a slight delay for the finite speed of the computer, but errors will not accumulate.

function StartAtEachSecond ()
{
    var OneSecond  = 1000; // milliseconds
    var MinInteral =   50; // milliseconds, estimated safe interval
    var StartTime = OneSecond - (new Date ()).getMilliseconds (); // Time until next second starts.
    if (StartTime < MinInteral) StartTime += OneSecond
    window.setTimeout (StartAtEachSecond, StartTime + MinInteral); // To set up the second after the next.
    for (var Delay = 0.0; Delay < OneSecond - MinInteral; Delay += RefreshInterval) 
    {
        window.setTimeout (UpdateAll, StartTime + Delay); // Runs during the next second.
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.