javascript setInterval

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)

Leave a Comment