Window setTimeout() method
- Previous Page setInterval()
- Next Page status
- Go to the Previous Level Window Object
Definition and Usage
setTimeout()
The method calls the function after a certain number of milliseconds.
Note:1 second = 1000 milliseconds.
Hint
setTimeout()
to execute only once.
If you need to repeat the execution, use setInterval()
.
Please use clearTimeout() method to prevent the function from starting.
To clear the timeout, use setTimeout()
Returned id:
myTimeout = setTimeout(function, milliseconds);
Then you can stop the function by calling clearTimeout() method to stop the execution:
clearTimeout(myTimeout);
See also:
Example
Example 1
A greeting that waits for 5 seconds:
const myTimeout = setTimeout(myGreeting, 5000);
Example 2
Use clearTimeout(myTimeout) to prevent the execution of myGreeting:
const myTimeout = setTimeout(myGreeting, 5000); function myStopFunction() { clearTimeout(myTimeout); }
More examples are provided below the page.
Syntax
setTimeout(function, milliseconds, param1, param2, ...)
parameters
parameters | description |
---|---|
function | Required. The function to be executed. |
milliseconds |
Optional. The number of milliseconds to wait before execution. Default value is 0. |
param1, param2,... |
Optional. Parameters passed to the function. Not supported in IE9 and earlier versions. |
return value
type | description |
---|---|
number. |
the timer ID. Please associate this id with clearTimeout(idmethod used together to cancel the timer. |
Browser support
is supported by all browsers setTimeout()
:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Supports | Supports | Supports | Supports | Supports | Supports |
More examples
Example 3
Display a warning box after 3 seconds (3000 milliseconds):
let timeout; function myFunction() { timeout = setTimeout(alertFunc, 3000); } function alertFunc() { alert("Hello!"); }
Example 4
Display timing text:
let x = document.getElementById("txt"); setTimeout(function(){ x.value = "2 seconds" }, 2000); setTimeout(function(){ x.value = "4 seconds" }, 4000); setTimeout(function(){ x.value = "6 seconds" }, 6000);
Example 5
Open a new window and close it after three seconds (3000 milliseconds):
const myWindow = window.open("", "", "width=200, height=100"); setTimeout(function() {myWindow.close()}, 3000);
Example 6
Continuous counting - but counting can be stopped:
function startCount() function stopCount()
Example 7
Clock created using timer events:
function startTime() { const date = new Date(); document.getElementById("txt").innerHTML = date.toLocaleTimeString(); setTimeout(function() {startTime()}, 1000); }
Example 8
Pass parameters to the function (does not work in IE9 and earlier versions):
setTimeout(myFunc, 2000, "param1", "param2");
Example 9
But if you use an anonymous function, it will work with all browsers:
setTimeout(function() {myFunc("param1", "param2")}, 2000);
- Previous Page setInterval()
- Next Page status
- Go to the Previous Level Window Object