YouTip LogoYouTip

Met Win Settimeout

```html Window setTimeout() Method Image 1: Window Object Reference Manual Window Object

Example

Pop up "Hello" after 3 seconds (3000 milliseconds):

setTimeout(function(){alert("Hello"); }, 3000);
Try it Β»

Definition and Usage

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.

Note: 1000 milliseconds = 1 second.

Note: To execute the function repeatedly, use the setInterval() method.

Note: 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
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 string containing the code to be executed, or a function to be executed.
milliseconds Optional. The time, in milliseconds, that the execution of code/function is delayed. Default is 0.
param1, param2, ... Optional. Additional parameters to pass to the function (Not supported in IE9 and earlier).

Technical Details

Returns: Returns an ID (number), which can be used with clearTimeout() to cancel the execution.

More Examples

Example

Pop up "Hello" after 3 seconds (3000 milliseconds):

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

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 Β»

Example

Open a new window and close it after 3 seconds:

var myWindow = window.open("", "", "width=200, height=100");
myWindow.document.write("

This is a new window

"); setTimeout(function(){myWindow.close()}, 3000);
Try it Β»

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 Β»

Example

Counter -- can be stopped by clicking the button:

function startCount()
function stopCount()
Try it Β»

Example

Show the current time:

function startTime(){
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    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 Β»

Example

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

var myVar;
function myStartFunction(){
    myVar = setTimeout(alertFunc, 2000, "", "Google");
}
Try it Β»

However, if using an anonymous function, all browsers support:

var myVar;
function myStartFunction(){
    myVar = setTimeout(function(){alertFunc("", "Google"); }, 2000);
}
Try it Β»

Related Pages

Window Object: setInterval() Method

Window Object: setTimeout() Method

Window Object: clearTimeout() Method

Image 2: Window Object Reference Manual Window Object ```
← Prop Screen AvailheightMet Win Setinterval β†’