devxlogo

Using the SetTimeout() Function

Using the SetTimeout() Function

Browsers Targeted: Internet Explorer 3+, Netscape 3 and 4

Sometimes, you need to set up events to happen at periodic intervals. Perhaps the simplest application of this is a clock. While both JavaScript and VBScript have time functions (through the Date object), a clock requires that you update this time regularly. As there is no periodic event that you can automatically query thrown by the document, one way of updating such a clock would be to use the SetTimeout() function. Taking the three arguments of an expression to call (as a string), the number of milliseconds to wait and an optional language specifier for interpreting the expression (the default value for this is “JavaScript”), the setTimeout function returns a numeric id that identifies the timeout. Then, after the requested interval, the expression is called in the specified language.

You can kill a timeout by passing the ID of the timeout to the function clearTimeout(). This is especially useful for making sure that the system doesn’t attempt to call a setTimeout function on a different page after you’ve left the original page.

0:00:00
 

 
0:00:00

 

devx-admin

Share the Post: