Reputation: 9158
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?
Upvotes: 18
Views: 32761
Reputation: 21
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.
}
}
Upvotes: 0
Reputation: 81384
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)
Upvotes: 33
Reputation: 7536
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.
Upvotes: 2
Reputation: 11538
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)
Upvotes: 0
Reputation: 91892
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.
Upvotes: 0
Reputation: 237817
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);
Upvotes: 0
Reputation: 39055
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.
Upvotes: 1