Javascript Reference
Categories: window

javascript window setTimeout( )

@July 25, 2008, 9:31 a.m.
setTimeout( ) Firefox/Netscape/NN 2 IE 3 Chrome/Safari/DOM n/a  

setTimeout("scriptExpression", msecs[, language]) setTimeout(functionReference, msecs[, arg1, ..., argN])

Starts a one-time timer that invokes the scriptExpression or functionReference after a delay of msecs. Other scripts can run while the browser waits to invoke the expression. A statement that sets the timer would look like the following:

timeoutID = setTimeout("finishWindow( )", 50);

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 setTimeout( ) method is invoked. Therefore, if you concatenate variables into this expression, their values must be ready when the setTimeout( ) 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 clearTimeout( ) method to stop the timer before it expires and invokes the delayed action.

The setTimeout( ) method can be made to behave like the setInterval( ) method in some constructions. If you place a setTimeout( ) method as the last statement of a function and direct the method to invoke the very same function, you can create looping execution with a timed delay between executions. This is how earlier browsers (before the setInterval( ) method was available) scripted repetitive tasks, such as displaying updated digital clock displays in form fields or the status bar.

 
Parameters
 
scriptExpression

Any script expression as a string, but most commonly a function. The function name with parentheses is placed inside the parameter's quoted string.

functionReference

Nonstring function reference (function name without the parentheses).

msecs

The time in milliseconds between invocations of the expression or functionReference.

args

An optional comma-delimited list of parameters to be passed to a function used as the functionReference parameter. Navigator only.

language

An optional scripting language specification of the expression parameter (default is JavaScript). IE for Windows only.

 
Returned Value

Integer acting as an identifier.

 


Powered by Linode.