|Property/method value type:||Number primitive|
|-||myWindow.setTimeout(aFunction, aWaitTime, someArguments)|
|-||myWindow.setTimeout(aFunction, aWaitTime, someArguments, aLanguage)|
|-||myWindow.setTimeout(aSourceText, aWaitTime, someArguments)|
|-||myWindow.setTimeout(aSourceText, aWaitTime, someArguments, aLanguage)|
|-||setTimeout(aFunction, aWaitTime, someArguments)|
|-||setTimeout(aFunction, aWaitTime, someArguments, aLanguage)|
|-||setTimeout(aSourceText, aWaitTime, someArguments)|
|-||setTimeout(aSourceText, aWaitTime, someArguments, aLanguage)|
|Argument list:||aFunction||A function object|
|aLanguage||A scripting language to execute the script source (MSIE only)|
|aWaitTime||A time interval in milliseconds|
|aSourceText||Some valid script source text|
|someArguments||The arguments to the function object (not supported in MSIE version 4)|
The setTimeout() method provides a way to defer the execution of a fragment of script source text. It is analogous to the eval() method with a delay before execution. Although this method returns an ID value that can then be used with the clearTimeout() method to cancel the execution, it is very likely that the deferred code will have executed already. You should set a flag accordingly so that you can avoid killing a deferred task that has already been completed. Attempting to kill deferred tasks that are no longer pending can crash your browser.
Code executed by this deferred mechanism will only be executed once. If you want it to be executed continuously then setInterval() is a better alternative. On the other hand, you may want to conditionally defer it again in which case you should call a handler function and, before exiting it, make another call to setTimeout() to activate another deferred task.
As is the case with setInterval() and eval(), you can execute multiple statements as long as they are separated by semi-colons.
Passing a function as one of the arguments is supported by MSIE in version 5 upwards and by Netscape Navigator in version 4 upwards. The scripting language can only be defined in MSIE.
This facility may be used to present a message in the status bar and then clear it again after some period of time has elapsed. It can be used to generate some animation in the status bar, although many people consider this to be a design cliche and much overused. If you do animate the status bar, you should consider whether it is useful and not distracting.
These timed animations are generally best triggered by an onLoad event handler.
Note that the deferred code is executed in the context and scope chain of the window object that received the method call to set up the deferred task.
The example is a cut down version of the ticker script used in the BBC News Online web site. The display techniques are the same but the example only shows one story. To see the real ticker in operation, refer to http://www.bbc.co.uk/news and view it with an MSIE browser. In the News Online ticker, many coding compromises were necessary to work round object boundary bugs in the MSIE for Macintosh browser. Because the ticker is constantly being updated, the object boundary is changing all the time and although this was played in an <IFRAME>, the mouse enter and mouse out events caused the MSIE browser to crash frequently. Earlier versions of the ticker also did a large amount of string creation/destruction which caused somewhat massive memory leaks. You can alleviate this by using meta refresh tags to force the garbage collection to happen.
Be careful how you operate on these interval timers. You can store an identifier, which you can later use to cancel the timer. However, if the timer has already been fired, some browsers may crash due to you trying to cancel a non-existent timer.
This method leaks somewhat badly in Netscape 2.