Window setTimeout()Method
Example
Display "Hello" after 3 seconds (3000 milliseconds):
Try it Yourself »
Definition and Usage
The setTimeout() method is used to call a function or evaluate an expression after a specified number of milliseconds.
Tip:1000 milliseconds = 1 second.
Tip:If you only want to repeat execution, you can use thesetInterval()method.
Tip:Use the clearTimeout()method to prevent the function from executing.
Browser Support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| setTimeout() | 1.0 | 4.0 | 1.0 | 1.0 | 4.0 |
Syntax
setTimeout(code, milliseconds, param1, param2, ...) setTimeout(function, milliseconds, param1, param2, ...)
| Parameter | Description |
|---|---|
| code/function | Required. A string of code to be executed, or a function. |
| milliseconds | Optional. The time, in milliseconds, to wait before executing or calling code/function. Default is 0. |
| param1, param2, ... | Optional. Additional parameters to pass to the executed function (not supported in IE9 and earlier versions). |
Technical Details
| Return Value: | Returns an ID (number). This ID can be passed to clearTimeout() to cancel execution. |
|---|
More Examples
Example
Display "Hello" after 3 seconds (3000 milliseconds):
Try it Yourself »
Example
Change the text in the input field at 2, 4, and 6 seconds:
Try it Yourself »
Example
Open a new window and close it after 3 seconds:
Try it Yourself »
Example
Use clearTimeout() to prevent the function from executing:
Try it Yourself »
Example
Counter -- you can stop it by clicking a button:
Try it Yourself »
Example
Display the current time:
Try it Yourself »
Example
Pass parameters to the alertFunc function (not supported in IE9 and earlier versions):
Try it Yourself »
However, if an anonymous function is used, all browsers support it:
Try it Yourself »
Related Pages
Window Object:setInterval() Method
Window Object:setTimeout() Method
Window Object:clearTimeout() Method
Other Extensions
Window Object