Javascript Reference
Categories: window

javascript window setInterval( )

@May 11, 2009, 8:06 a.m.
setInterval( ) Firefox/Netscape/NN 4 IE 4 Chrome/Safari/DOM n/a  
setInterval("scriptExpression", msecs[, language]) setInterval(functionReference, msecs[, arg1, ..., argN])

Starts a timer that continually invokes the expression every msecs. Other scripts can run in the time between calls to expression. This method is useful for starting animation sequences that must reposition an element along a path at a fixed rate of speed. A repetitive call to an animation function would look like the following:

intervalID = setInterval("advanceAnimation( )", 500);

The parameter situation can be confusing. The simplest, most cross-browser approach is to invoke a script function (as a string), with the interval time (in milliseconds) as the second parameter. Any script expression will execute, but the expression is evaluated at the time the setInterval( ) method is invoked. Therefore, if you concatenate variables into this expression, their values must be ready when the setInterval( ) method runs, even though the variables won't be used until some milliseconds later.

IE permits a third parameter to specify a different scripting language in which the expression is to run. Unless it is a VBScript expression, you can omit this parameter. Navigator, however, lets you substitute a function reference (not a string) as the first parameter, and pass a comma-delimited list of parameters that go to the function call. These parameters go after the msecs time, and they can be any data types.

This method returns an ID that should be saved as a global variable and be available as the parameter for the clearInterval( ) method to stop the looping timer. Unless you explicitly clear the interval process, it will continue to execute until the page unloads.

  • Any script expression as a string, but most commonly a function. The function name with parentheses is placed inside the parameter's quoted string.
  • Nonstring function reference (function name without the parentheses).
  • The time in milliseconds between invocations of the expression or functionReference.
  • An optional comma-delimited list of parameters to be passed to a function used as the functionReference parameter. Navigator only.
  • An optional scripting language specification of the expression parameter (default is JavaScript). IE for Windows only.
Returned Value

Integer acting as an identifier for this repetitive process.

Powered by Linode.