English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
setTimeout()method to set a timer, which will call the function or execute the code block once the timer expires.
This method returns a unique identifier for the timer's timeout ID, so you can later callclearTimeout()to delete it.
UseclearTimeout()method to prevent the function from running.
This function executes only once. If you need to execute it repeatedly, please usesetInterval()Method.
window.setTimeout(function, delay, param1, param2, ...)
setTimeout(function() { alert("Hello World"); }, 2000);Test and See‹/›
The numbers in the table specify the first browser version that fully supports the setTimeout() method:
Method | |||||
setTimeout() | 1 | 1 | 4 | 1 | 4 |
Parameters | Description |
---|---|
function | (Required) The function to be executed after the timer expires |
delay | (Required) The time the timer should wait (in milliseconds) (1000 milliseconds= 1seconds), then execute the specified function or code. If omitted, a value of 0 |
param1param2, param | , ...(Optional) Pass thisfunction(function)9and earlier versions do not support) the other parameters (IE |
Return value: | A number representing the timeout ID value set for the timer. This value can be used with clearTimeout() to cancel the timer |
---|
This example references an external "named" function:
var timeoutID; function myFunc() { timeoutID = setTimeout(myCallback, 2000); } function myCallback() { alert("Hello World"); }Test and See‹/›
Prevent the function from running using clearTimeout():
var timeoutID; function myFunc() { timeoutID = setTimeout(myCallback, 2000); } function myCallback() { alert("Hello World"); } function myStopFunc() { clearTimeout(timeoutID); }Test and See‹/›
Display the current time recursively (like a digital watch, every1Execute the "startTimer()" function once per second):
window.addEventListener("load", startTimer); function startTimer() { var date = new Date(); var x = document.getElementById("result"); x.innerHTML = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); setTimeout(startTimer, 1000); // Recursion }Test and See‹/›
Create a dynamic progress bar using setTimeout():
var i = 0; var bar = document.getElementById("progress",-bar); var t; function start() { if(i < 100) { i++; bar.style.width = i + "%"; bar.innerHTML = i + "%"; t = setTimeout(start, 60); // Recursion } } function stop() { clearTimeout(t); }Test and See‹/›
Click the "Start Counting" button below to start the timer. Click the "Stop Counting" button to stop counting:
Pass parameters to the myFunc function (in IE9and does not work in earlier versions):
var timeoutID = setTimeout(myFunc, 2000, "First", "Second", "Third");Test and See‹/›
However, if you use an anonymous function, it will work in all browsers:
var timeoutID = setTimeout(function() { myFunc("First", "Second", "Third"); }, 2000);Test and See‹/›
Window (Window) Reference:clearTimeout() method
Window (Window) Reference:setInterval() method
Window (Window) Reference:clearInterval() method