Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil) . Junte-se a nós e ajude a fazer o trabalho!
Sets a timer which executes a function or specified piece of code once after the timer expires.
Syntax
var timeoutID = window.setTimeout(func[, delay, param1, param2, ...]); var timeoutID = window.setTimeout(code[, delay]);
Parameters
func
- A
function
to be executed after the timer expires. code
- An optional syntax allows you to include a string instead of a function, which is compiled and executed when the timer expires. This syntax is not recommended for the same reasons that make using
eval()
a security risk. delay
Optional- The time, in milliseconds (thousandths of a second), the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used. Note that the actual delay may be longer; see Reasons for delays longer than specified below.
param1, ..., paramN
Optional- Additional parameters which are passed through to the function specified by func once the timer expires.
Passing additional parameters to the function in the first syntax does not work in Internet Explorer 9 and below. If you want to enable this functionality on that browser, you must use a polyfill (see the Callback arguments section).
Return value
The returned timeoutID
is a numeric, non-zero value which identifies the timer created by the call to setTimeout()
; this value can be passed to Window.clearTimeout()
to cancel the timeout.
It may be helpful to be aware that setTimeout()
and setInterval()
share the same pool of IDs, and that clearTimeout()
and clearInterval()
can technically be used interchangeably. For clarity, however, you should try to always match them to avoid confusion when maintaining your code.
Example
The following example sets up two simple buttons in a web page and hooks them to the setTimeout()
and clearTimeout()
routines. Pressing the first button will set a timeout which calls an alert dialog after two seconds and stores the timeout id for use by clearTimeout()
. You may optionally cancel this timeout by pressing on the second button.
HTML content
<p>Live Example</p> <button onclick="delayedAlert();">Show an alert box after two seconds</button> <p></p> <button onclick="clearAlert();">Cancel alert before it happens</button>
JavaScript content
var timeoutID; function delayedAlert() { timeoutID = window.setTimeout(slowAlert, 2000); } function slowAlert() { alert("That was really slow!"); } function clearAlert() { window.clearTimeout(timeoutID); }
Results
See also clearTimeout()
example.
Polyfill
If you need to pass one or more arguments to your callback function, but need it to work in browsers which don't support sending additional parameters using either setTimeout()
or setInterval() (e.g.
Internet Explorer 9 and below)
, you can include this polyfill which enables the HTML5 standard parameter-passing functionality. Just add this code to the top of your script:
/*\ |*| |*| Polyfill which enables the passage of arbitrary arguments to the |*| callback functions of JavaScript timers (HTML5 standard syntax). |*| |*| https://developer.mozilla.org/en-US/docs/DOM/window.setInterval |*| |*| Syntax: |*| var timeoutID = window.setTimeout(func, delay[, param1, param2, ...]); |*| var timeoutID = window.setTimeout(code, delay); |*| var intervalID = window.setInterval(func, delay[, param1, param2, ...]); |*| var intervalID = window.setInterval(code, delay); |*| \*/ (function() { setTimeout(function(arg1) { if (arg1 === 'test') { // feature test is passed, no need for polyfill return; } var __nativeST__ = window.setTimeout; window.setTimeout = function(vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */ ) { var aArgs = Array.prototype.slice.call(arguments, 2); return __nativeST__(vCallback instanceof Function ? function() { vCallback.apply(null, aArgs); } : vCallback, nDelay); }; }, 0, 'test'); var interval = setInterval(function(arg1) { clearInterval(interval); if (arg1 === 'test') { // feature test is passed, no need for polyfill return; } var __nativeSI__ = window.setInterval; window.setInterval = function(vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */ ) { var aArgs = Array.prototype.slice.call(arguments, 2); return __nativeSI__(vCallback instanceof Function ? function() { vCallback.apply(null, aArgs); } : vCallback, nDelay); }; }, 0, 'test'); }())
IE only fix
If you want a completely unobtrusive hack for every other mobile or desktop browser, including IE 9 and below, you can either use JavaScript conditional comments:
/*@cc_on // conditional IE < 9 only fix @if (@_jscript_version <= 9) (function(f){ window.setTimeout=f(window.setTimeout); window.setInterval=f(window.setInterval); })(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c instanceof Function?c.apply(this,a):eval(c)},t)}}); @end @*/
Or go for a very clean approach based on the IE HTML conditional feature:
<!--[if lte IE 9]><script> (function(f){ window.setTimeout=f(window.setTimeout); window.setInterval=f(window.setInterval); })(function(f){return function(c,t){ var a=[].slice.call(arguments,2);return f(function(){c instanceof Function?c.apply(this,a):eval(c)},t)} }); </script><![endif]-->
Workarounds
Another possibility is to use an anonymous function to call your callback, but this solution is a bit more expensive. Example:
var intervalID = setTimeout(function() { myFunc("one", "two", "three"); }, 1000);
The above example can also be written with the help of an arrow function:
var intervalID = setTimeout(() => { myFunc("one", "two", "three"); }, 1000);
Yet another possibility is to use function's bind
. Example:
setTimeout(function(arg1){}.bind(undefined, 10), 1000);
The "this
" problem
When you pass a method to setTimeout()
(or any other function, for that matter), it will be invoked with a this
value that may differ from your expectation. This issue is explained in detail in the JavaScript reference.
Explanation
Code executed by setTimeout()
is called from a separate execution context to the function from which setTimeout
was called. The usual rules for setting the this
keyword for the called function apply, and if you have not set this
in the call or with bind
, it will default to the global
(or window
) object in non–strict mode, or be undefined in strict mode. It will not be the same as the this
value for the function that called setTimeout
. See the following example:
myArray = ["zero", "one", "two"]; myArray.myMethod = function (sProperty) { alert(arguments.length > 0 ? this[sProperty] : this); }; myArray.myMethod(); // prints "zero,one,two" myArray.myMethod(1); // prints "one"
The above works because when myMethod
is called, its this
is set to myArray
by the call, so within the function, this[sProperty]
is equivalent to myArray[sProperty]
. However, in the following:
setTimeout(myArray.myMethod, 1000); // prints "[object Window]" after 1 second setTimeout(myArray.myMethod, 1500, "1"); // prints "undefined" after 1.5 seconds
The myArray.myMethod
function is passed to setTimeout
, then when it's called, its this
is not set so it defaults to the window
object. There's also no option to pass a thisArg
to setTimeout as there is in Array methods like forEach, reduce, etc. and as shown below, using call
to set this
doesn't work either.
setTimeout.call(myArray, myArray.myMethod, 2000); // error: "NS_ERROR_XPC_BAD_OP_ON_WN_PROTO: Illegal operation on WrappedNative prototype object" setTimeout.call(myArray, myArray.myMethod, 2500, 2); // same error
Possible solutions
A common way to solve the problem is to use a wrapper function that sets this
to the required value:
setTimeout(function(){myArray.myMethod()}, 2000); // prints "zero,one,two" after 2 seconds setTimeout(function(){myArray.myMethod('1')}, 2500); // prints "one" after 2.5 seconds
Arrow functions are a possible alternative, too:
setTimeout(() => {myArray.myMethod()}, 2000); // prints "zero,one,two" after 2 seconds setTimeout(() => {myArray.myMethod('1')}, 2500); // prints "one" after 2.5 seconds
Another possible way to solve the "this
" problem is to replace the host setTimeout()
and setInterval()
global functions with ones that allow passing a this
object and set it in the callback using Function.prototype.call
, e.g.:
// Enable setting 'this' in JavaScript timers var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval; window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) { var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2); return __nativeST__(vCallback instanceof Function ? function () { vCallback.apply(oThis, aArgs); } : vCallback, nDelay); }; window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) { var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2); return __nativeSI__(vCallback instanceof Function ? function () { vCallback.apply(oThis, aArgs); } : vCallback, nDelay); };
New feature test:
myArray = ["zero", "one", "two"]; myArray.myMethod = function (sProperty) { alert(arguments.length > 0 ? this[sProperty] : this); }; setTimeout(alert, 1500, "Hello world!"); // the standard use of setTimeout and setInterval is preserved, but... setTimeout.call(myArray, myArray.myMethod, 2000); // prints "zero,one,two" after 2 seconds setTimeout.call(myArray, myArray.myMethod, 2500, 2); // prints "two" after 2.5 seconds
Function.prototype.bind()
method to set the value of this
for all calls to a given function. This can avoid having to use a wrapper function to set the value of this
in a callback.Example using bind()
:
myArray = ["zero", "one", "two"]; myBoundMethod = (function (sProperty) { console.log(arguments.length > 0 ? this[sProperty] : this); }).bind(myArray); myBoundMethod(); // prints "zero,one,two" because 'this' is bound to myArray in the function myBoundMethod(1); // prints "one" setTimeout(myBoundMethod, 1000); // still prints "zero,one,two" after 1 second because of the binding setTimeout(myBoundMethod, 1500, "1"); // prints "one" after 1.5 seconds
Notes
Timeouts are cancelled using Window.clearTimeout()
. To call a function repeatedly (e.g., every N milliseconds), consider using Window.setInterval()
.
It's important to note that the function or code snippet cannot be executed until the thread that called setTimeout()
has terminated. For example:
function foo(){ console.log('foo has been called'); } setTimeout(foo, 0); console.log('After setTimeout');
Will write to the console:
After setTimeout foo has been called
Because even though setTimeout
was called with a delay of zero, it's placed on a queue and scheduled to run at the next opportunity, not immediately. Currently executing code must complete before functions on the queue are executed, the resulting execution order may not be as expected.
Passing string literals
Passing a string instead of a function to setTimeout()
suffers from the same hazards as using eval.
// Recommended window.setTimeout(function() { alert("Hello World!"); }, 500); // Not recommended window.setTimeout("alert('Hello World!');", 500);
A string passed to setTimeout
is evaluated in the global context, so local symbols in the context where setTimeout()
was called will not be available when the string is evaluated as code.
Reasons for delays longer than specified
There are a number of reasons why a timeout may take longer to fire than anticipated. This section describes the most common reasons.
Nested timeouts forced to >=4ms
Historically browsers implement setTimeout()
"clamping": successive setTimeout()
calls with delay
smaller than the "minimum delay" limit are forced to use at least the minimum delay. The minimum delay, DOM_MIN_TIMEOUT_VALUE
, is 4 ms (stored in a preference in Firefox: dom.min_timeout_value
), with a DOM_CLAMP_TIMEOUT_NESTING_LEVEL
of 5.
In fact, 4 ms is specified by the HTML5 spec and is consistent across browsers released in 2010 and onward. Prior to (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the minimum timeout value for nested timeouts was 10 ms.
To implement a 0 ms timeout in a modern browser, you can use window.postMessage()
as described here.
Timeouts in inactive tabs clamped to >=1000ms
To reduce the load (and associated battery usage) from background tabs, timeouts are often clamped to firing no more often than once per second (1000 ms) in inactive tabs.
Firefox implements this behavior since version 5 (see bug 633421, the 1000ms constant can be tweaked through the dom.min_background_timeout_value
preference). Chrome implements this behavior since version 11 (crbug.com/66078).
Firefox for Android uses a timeout value of 15 minutes for background tabs since bug 736602 in Firefox 14, and background tabs can also be unloaded entirely.
Firefox 50 no longer throttles background tabs if a Web Audio API AudioContext
is actively playing sound. Firefox 51 further amends this such that background tabs are no longer throttled if an AudioContext
is present in the tab at all, even if no sound is being played. These resolve a number of issues with apps which play note-based music not being able to time or synchronize the music properly when the tab is in the background.
Late timeouts
In addition to "clamping", the timeout can also fire later when the page (or the OS/browser itself) is busy with other tasks
Maximum delay value
Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally. This causes an integer overflow when using delays larger than 2147483647, resulting in the timeout being executed immediately.
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'WindowTimers.setTimeout()' in that specification. |
Living Standard | Initial definition (DOM Level 0) |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1.7 or earlier) | 4.0 | 4.0 | 1.0 |
Supports parameters for callback[1] | (Yes) | (Yes) | 10.0 | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 1.0 | 1.0 | 1.0 (1) | 6.0 | 6.0 | 1.0 |
Supports parameters for callback[1] | ? | ? | ? | ? | ? | ? |
[1] Whether it supports the optional parameters when in its first form or not.