setTimeout()
Method
Example
Display "Hello" after 3 seconds (3000 milliseconds):
setTimeout(function(){ alert("Hello"); }, 3000);
Definition and Usage
The setTimeout()
method calls a function or evaluates an expression after a specified number of milliseconds.
Tip: 1000 milliseconds = 1 second.
Tip: If you want to repeat execution, use the setInterval() method.
Tip: Use the clearTimeout() method to prevent the function from running.
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
Method | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
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. The code to be called, or the function. |
milliseconds | Optional. The number of milliseconds to wait before executing the code/function. Default is 0. |
param1, param2, ... | Optional. Additional parameters to pass to the function (not supported in IE9 and earlier). |
Technical Details
| Return Value: | Returns an ID (number) that can be used to cancel the execution with clearTimeout()
. |
| --- | --- |
More Examples
Example
Display "Hello" after 3 seconds (3000 milliseconds):
var myVar;
function myFunction() {
myVar = setTimeout(alertFunc, 3000);
}
function alertFunc() {
alert("Hello!");
}
Example
Change the text in an input box at 2, 4, and 6 seconds:
var 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
Open a new window and close it after 3 seconds:
var myWindow = window.open("", "", "width=200, height=100");
myWindow.document.write("<p>This is a new window</p>");
setTimeout(function(){ myWindow.close() }, 3000);
Example
Prevent the function from executing using clearTimeout()
:
var myVar;
function myFunction() {
myVar = setTimeout(function(){ alert("Hello") }, 3000);
}
function myStopFunction() {
clearTimeout(myVar);
}
Example
Counter that can be stopped by clicking a button:
function startCount()
function stopCount()
Example
Display the current time:
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// Add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
document.getElementById("txt").innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function(){ startTime() }, 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
Example
Pass parameters to the alertFunc
function (not supported in IE9 and earlier):
var myVar;
function myStartFunction() {
myVar = setTimeout(alertFunc, 2000, "tutorialpro", "Google");
}
However, all browsers support using an anonymous function:
var myVar;
function myStartFunction() {
myVar = setTimeout(function(){ alertFunc("tutorialpro", "Google"); }, 2000);
}
Related Pages
Window Object: setInterval() Method
Window Object: setTimeout() Method
Window Object: clearTimeout() Method