Window setTimeout()Method

Window 对象参考手册Window Object

Example

Display "Hello" after 3 seconds (3000 milliseconds):

setTimeout(function(){ alert("Hello"); }, 3000);

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):

var myVar; function myFunction() { myVar = setTimeout(alertFunc, 3000); } function alertFunc() { alert("Hello!"); }

Try it Yourself »

Example

Change the text in the input field 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);

Try it Yourself »

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);

Try it Yourself »

Example

Use clearTimeout() to prevent the function from executing:

var myVar; function myFunction() { myVar = setTimeout(function(){ alert("Hello") }, 3000); } function myStopFunction() { clearTimeout(myVar); }

Try it Yourself »

Example

Counter -- you can stop it by clicking a button:

function startCount() function stopCount()

Try it Yourself »

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 0 before numbers less than 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; }

Try it Yourself »

Example

Pass parameters to the alertFunc function (not supported in IE9 and earlier versions):

var myVar; function myStartFunction() { myVar = setTimeout(alertFunc, 2000, "Example", "Google"); }

Try it Yourself »

However, if an anonymous function is used, all browsers support it:

var myVar; function myStartFunction() { myVar = setTimeout(function(){ alertFunc("Example", "Google"); }, 2000); }

Try it Yourself »


Related Pages

Window Object:setInterval() Method

Window Object:setTimeout() Method

Window Object:clearTimeout() Method

Window 对象参考手册Window Object

Other Extensions