window.setTimeout

  • Revision slug: DOM/window.setTimeout
  • Revision title: window.setTimeout
  • Revision id: 301723
  • Created:
  • Creator: Tonymec
  • Is current revision? No
  • Comment removing duplicate subtitle which was obscuring the "Inactive tabs" section under "Notes"

Revision Content

{{ DomRef() }}

Summary

Calls a function or executes a code snippet after specified delay.

Syntax

var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);

where

  • timeoutID is the numerical ID of the timeout, which can be used later with window.clearTimeout.
  • func is the function you want to execute after delay milliseconds.
  • code in the alternate syntax, is a string of code you want to execute after delay milliseconds (using this syntax is not recommended for the same reasons as using eval())
  • delay is the number of milliseconds (thousandths of a second) that the function call should be delayed by. The actual delay may be longer, see Notes below.

Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer. If you want to enable this functionality on that browser you must use a compatibility code (see the Callback arguments paragraph).

Note: Prior to Gecko 13 {{ geckoRelease("13.0") }}, Gecko passed an extra parameter to the callback routine, indicating the "actual lateness" of the timeout in milliseconds. This non-standard parameter is no longer passed.

Examples

function generateOutput(aConcise) {
    if(aConcise) {
        parent.generateConciseOutput();
    } else {
        parent.generateOutput();
    }
}
window.setTimeout(generateOutput, 1000, true);  // won't pass "true" to the generateOutput in IE
// Increase compatibility with unnamed functions
setTimeout(function() {
  generateOutput(true);
}, 1000);  // will work with every browser
window.setTimeout("window.parent.generateOutput(true)", 1000);  // deprecated

{{ JSFiddleLink("madBYK/BjjBc") }}

See also clearTimeout() example.

Callback arguments

If you need to pass an argument to your callback function, but need it to work in Internet Explorer, which doesn't support sending additional parameters (neither with setTimeout() or setInterval()) you can include this IE-specific compatibility code which will enable the HTML5 standard parameters passage functionality in that browser for both timers just by inserting it at the beginning of your scripts.

/*\
|*|
|*|  IE-specific 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);
|*|
\*/

if (document.all && !window.setTimeout.isPolyfill) {
  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);
  };
  window.setTimeout.isPolyfill = true;
}

if (document.all && !window.setInterval.isPolyfill) {
  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);
  };
  window.setInterval.isPolyfill = true;
}

Another possibility is to use an anonymous function to call your callback, but his solution is a bit more expensive. Example:

var intervalID = setTimeout(function() { myFunc("one", "two", "three"); }, 1000);

The "this" problem

When you pass a method to setTimeout() (or any other function, for that matter), it will be invoked with a wrong this value. This problem is explained in detail in the JavaScript reference.

Explanation

Code executed by setTimeout() is run in a separate execution context to the function from which it was called. As a consequence, the this keyword for the called function will be set to the window (or global) object, 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"
setTimeout(myArray.myMethod, 1000); // prints "[object Window]" after 1 second
setTimeout(myArray.myMethod, 1500, "1"); // prints "undefined" after 1,5 seconds
// let's try to pass the 'this' object
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

As you can see there are no ways to pass the this object to the callback function.

A possible solution

A possible way to solve the "this" problem is to replace the two native setTimeout() or setInterval() global functions with two non-native ones which will enable their invocation through the Function.prototype.call method. The following example shows a possible replacement:

// Enable the passage of the 'this' object through the 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);
};
Note: This two replacements will also enable the HTML5 standard passage of arbitrary arguments to the callback functions of timers in IE. So they can be used as polyfills also. See the Callback arguments paragraph.

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

There are not native solutions ad hoc to this problem.

Note: JavaScript 1.8.5 introduces the Function.prototype.bind() method, which lets you specify the value that should be used as this for all calls to a given function. This lets you easily bypass problems where it's unclear what this will be, depending on the context from which your function was called.

Notes

You can cancel the timeout using window.clearTimeout().

If you wish to have your function called repeatedly (i.e. every N milliseconds), consider using window.setInterval().

Passing string literals

Passing a string instead of a function to setTimeout() suffers from the same hazards as using eval.

// Correct
window.setTimeout(function() {
    alert("Hello World!");
}, 500);

// Incorrect
window.setTimeout("alert(\"Hello World!\");", 500);

String literals are 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.

Minimum delay and timeout nesting

Historically browsers implement setTimeout() "clamping": successive setTimeout() calls with delay smaller than the "minimum delay" limit are forced to the 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 5ms.

In fact, 4ms is specified by the HTML5 spec and is consistent across browsers released in 2010 and onward. Prior to {{ geckoRelease("5.0") }}, the minimum timeout value for nested timeouts was 10 ms.

In addition to "clamping", the timeout can also fire later when the page (or the OS/browser itself) is busy with other tasks.

To implement a 0 ms timeout in a modern browser you can use {{ domxref("window.postMessage()") }} as described here.

Inactive tabs

In {{ geckoRelease("5.0") }} and Chrome 11, timeouts are clamped to firing no more often than once per second (1000ms) in inactive tabs; see {{ bug(633421) }} for more information about this in Mozilla or crbug.com/66078 for details about this in Chrome.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{ CompatGeckoDesktop("1") }} 4.0 4.0 1.0
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 1.0 1.0 {{ CompatGeckoMobile("1") }} 6.0 6.0 1.0

Specification

Part of DOM level 0, as specified in HTML5.

Revision Source

<div>
  {{ DomRef() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Calls a function or executes a code snippet after specified delay.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">
<em>var timeoutID</em> = window.setTimeout(<em>func</em>, <em>delay</em>, [<em>param1</em>, <em>param2</em>, ...]);
<em>var timeoutID</em> = window.setTimeout(<em>code</em>, <em>delay</em>);
</pre>
<p>where</p>
<ul>
  <li><code>timeoutID</code> is the <em>numerical</em> ID of the timeout, which can be used later with <a href="/en-US/docs/DOM/window.clearTimeout" title="en-US/docs/DOM/window.clearTimeout">window.clearTimeout</a>.</li>
  <li><code>func</code> is the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="en-US/docs/Core_JavaScript_1.5_Reference/Global_Objects/Function">function</a> you want to execute after <code>delay</code> milliseconds.</li>
  <li><code>code</code> in the alternate syntax, is a string of code you want to execute after <code>delay</code> milliseconds (using this syntax is <strong>not recommended</strong> for the same reasons as using <a href="/en-US/docs/JavaScript/Reference/Global_Objects/eval#Don%27t_use_eval!" title="en-US/docs/Core JavaScript 1.5 Reference/Global Functions/Eval#Don't use eval!">eval()</a>)</li>
  <li><code>delay</code> is the number of milliseconds (thousandths of a second) that the function call should be delayed by. The actual delay may be longer, see Notes below.</li>
</ul>
<p>Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer. If you want to enable this functionality on that browser you must use a compatibility code (see the <a href="#Callback_arguments">Callback arguments</a> paragraph).</p>
<div class="note">
  <strong>Note:</strong> Prior to Gecko 13 {{ geckoRelease("13.0") }}, Gecko passed an extra parameter to the callback routine, indicating the "actual lateness" of the timeout in milliseconds. This non-standard parameter is no longer passed.</div>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: js">
function generateOutput(aConcise) {
    if(aConcise) {
        parent.generateConciseOutput();
    } else {
        parent.generateOutput();
    }
}
window.setTimeout(generateOutput, 1000, true);  // won't pass "true" to the generateOutput in IE
</pre>
<pre class="brush: js">
// Increase compatibility with unnamed functions
setTimeout(function() {
  generateOutput(true);
}, 1000);  // will work with every browser
</pre>
<pre class="brush: js">
window.setTimeout("window.parent.generateOutput(true)", 1000);  // deprecated
</pre>
<p>{{ JSFiddleLink("madBYK/BjjBc") }}</p>
<p>See also <a href="/en-US/docs/DOM/window.clearTimeout#Example" title="en-US/docs/DOM/window.clearTimeout#Example"><code>clearTimeout()</code> example</a>.</p>
<h2 id="Callback_arguments">Callback arguments</h2>
<p>If you need to pass an argument to your callback function, but need it to work in Internet Explorer, which doesn't support sending additional parameters (neither with <code>setTimeout()</code> or <code>setInterval()</code>) you can include this <em>IE-specific</em> compatibility code which will enable the HTML5 standard parameters passage functionality in that browser for both timers just by inserting it at the beginning of your scripts.</p>
<pre class="brush: js">
/*\
|*|
|*|&nbsp; IE-specific 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
|*|
|*|&nbsp; Syntax:
|*|&nbsp; var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
|*|&nbsp; var timeoutID = window.setTimeout(code, delay);
|*|&nbsp; var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
|*|&nbsp; var intervalID = window.setInterval(code, delay);
|*|
\*/

if (document.all &amp;&amp; !window.setTimeout.isPolyfill) {
&nbsp; var __nativeST__ = window.setTimeout;
&nbsp; window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
&nbsp;&nbsp;&nbsp; var aArgs = Array.prototype.slice.call(arguments, 2);
&nbsp;&nbsp;&nbsp; return __nativeST__(vCallback instanceof Function ? function () {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vCallback.apply(null, aArgs);
&nbsp;&nbsp;&nbsp; } : vCallback, nDelay);
&nbsp; };
&nbsp; window.setTimeout.isPolyfill = true;
}

if (document.all &amp;&amp; !window.setInterval.isPolyfill) {
&nbsp; var __nativeSI__ = window.setInterval;
&nbsp; window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
&nbsp;&nbsp;&nbsp; var aArgs = Array.prototype.slice.call(arguments, 2);
&nbsp;&nbsp;&nbsp; return __nativeSI__(vCallback instanceof Function ? function () {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vCallback.apply(null, aArgs);
&nbsp;&nbsp;&nbsp; } : vCallback, nDelay);
&nbsp; };
&nbsp; window.setInterval.isPolyfill = true;
}
</pre>
<p>Another possibility is to use an anonymous function to call your callback, but his solution is a bit more expensive. Example:</p>
<pre class="brush: js">
var intervalID = setTimeout(function() { myFunc("one", "two", "three"); }, 1000);
</pre>
<h2 id="The_.22this.22_problem">The "<code>this</code>" problem</h2>
<p>When you pass a method to <code>setTimeout()</code> (or any other function, for that matter), it will be invoked with a wrong <code>this</code> value. This problem is explained in detail in the <a href="/en-US/docs/JavaScript/Reference/Operators/this#Method_binding" title="en-US/docs/Core_JavaScript_1.5_Reference/Operators/Special_Operators/this_Operator#Method_binding">JavaScript reference</a>.</p>
<h3 id="Explanation">Explanation</h3>
<p>Code executed by <code>setTimeout()</code> is run in a separate execution context to the function from which it was called. As a consequence, the <code>this</code> keyword for the called function will be set to the <code>window</code> (or <code>global</code>) object, it will not be the same as the <code>this</code> value for the function that called <code>setTimeout</code>. See the following example:</p>
<pre class="brush: js">
myArray = ["zero", "one", "two"];
myArray.myMethod = function (sProperty) {
    alert(arguments.length &gt; 0 ? this[sProperty] : this);
};

myArray.myMethod(); // prints "zero,one,two"
myArray.myMethod(1); // prints "one"
setTimeout(myArray.myMethod, 1000); // prints "[object Window]" after 1 second
setTimeout(myArray.myMethod, 1500, "1"); // prints "undefined" after 1,5 seconds
// let's try to pass the 'this' object
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</pre>
<p>As you can see there are no ways to pass the <code>this</code> object to the callback function.</p>
<h3 id="A_possible_solution">A possible solution</h3>
<p>A possible way to solve the "<code>this</code>" problem is to replace the two native <code>setTimeout()</code> or <code>setInterval()</code> global functions with two <em>non-native</em> ones which will enable their invocation through the <a href="en-US/docs/JavaScript/Reference/Global_Objects/Function/call" title="en-US/docs/JavaScript/Reference/Global_Objects/Function/call"><code>Function.prototype.call</code></a> method. The following example shows a possible replacement:</p>
<pre class="brush: js">
// Enable the passage of the 'this' object through the JavaScript timers
&nbsp;
var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
&nbsp;
window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
&nbsp; var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
&nbsp; return __nativeST__(vCallback instanceof Function ? function () {
&nbsp;&nbsp;&nbsp; vCallback.apply(oThis, aArgs);
&nbsp; } : vCallback, nDelay);
};
&nbsp;
window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
&nbsp; var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
&nbsp; return __nativeSI__(vCallback instanceof Function ? function () {
&nbsp;&nbsp;&nbsp; vCallback.apply(oThis, aArgs);
&nbsp; } : vCallback, nDelay);
};</pre>
<div class="note">
  <strong>Note:</strong> This two replacements will also enable the HTML5 standard passage of arbitrary arguments to the callback functions of timers in IE. So they can be used as polyfills also. See the <a href="#Callback_arguments">Callback arguments</a> paragraph.</div>
<p>New feature test:</p>
<pre class="brush: js">
myArray = ["zero", "one", "two"];
myArray.myMethod = function (sProperty) {
    alert(arguments.length &gt; 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
</pre>
<p>There are not native solutions <em>ad hoc</em> to this problem.</p>
<div class="note">
  <strong>Note:</strong> JavaScript 1.8.5 introduces the <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind" title="en-US/docs/JavaScript/Reference/Global Objects/Function/bind">Function.prototype.bind()</a></code> method, which lets you specify the value that should be used as <code>this</code> for all calls to a given function. This lets you easily bypass problems where it's unclear what this will be, depending on the context from which your function was called.</div>
<h2 id="Notes">Notes</h2>
<p>You can cancel the timeout using <code><a href="/en-US/docs/DOM/window.clearTimeout" title="en-US/docs/DOM/window.clearTimeout">window.clearTimeout()</a></code>.</p>
<p>If you wish to have your function called repeatedly (i.e. every N milliseconds), consider using <code><a href="/en-US/docs/DOM/window.setInterval" title="en-US/docs/DOM/window.setInterval">window.setInterval()</a></code>.</p>
<h3 id="Passing_string_literals">Passing string literals</h3>
<p>Passing a string instead of a function to <code>setTimeout()</code> suffers from the same hazards as using <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/eval#Don.27t_use_eval.21" title="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval">eval</a>. </code></p>
<pre class="brush: js">
// Correct
window.setTimeout(function() {
    alert("Hello World!");
}, 500);

// Incorrect
window.setTimeout("alert(\"Hello World!\");", 500);

</pre>
<p>String literals are evaluated in the global context, so local symbols in the context where <code>setTimeout()</code> was called will not be available when the string is evaluated as code.</p>
<h3 id="Minimum_delay_and_timeout_nesting">Minimum delay and timeout nesting</h3>
<p><a class="external" href="http://code.google.com/p/chromium/issues/detail?id=792#c10" title="http://code.google.com/p/chromium/issues/detail?id=792#c10">Historically</a> browsers implement <code>setTimeout()</code> "clamping": successive <code>setTimeout()</code> calls with <code>delay</code> smaller than the "minimum delay" limit are forced to the use at least the minimum delay. The minimum delay, <code>DOM_MIN_TIMEOUT_VALUE</code>, is 4 ms (stored in a preference in Firefox: <code>dom.min_timeout_value</code>), with a <code>DOM_CLAMP_TIMEOUT_NESTING_LEVEL</code> of 5ms.</p>
<p>In fact, 4ms is <a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers">specified by the HTML5 spec</a> and is consistent across browsers released in 2010 and onward. Prior to {{ geckoRelease("5.0") }}, the minimum timeout value for nested timeouts was 10 ms.</p>
<p>In addition to "clamping", the timeout can also fire later when the page (or the OS/browser itself) is busy with other tasks.</p>
<p>To implement a 0 ms timeout in a modern browser you can use {{ domxref("window.postMessage()") }} as <a class="external" href="http://dbaron.org/log/20100309-faster-timeouts" title="http://dbaron.org/log/20100309-faster-timeouts">described here</a>.</p>
<h4 id="Inactive_tabs">Inactive tabs</h4>
<p>In {{ geckoRelease("5.0") }} and Chrome 11, timeouts are clamped to firing no more often than once per second (1000ms) in inactive tabs; see {{ bug(633421) }} for more information about this in Mozilla or <a class="external" href="http://crbug.com/66078" title="http://crbug.com/66078">crbug.com/66078</a> for details about this in Chrome.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>{{ CompatGeckoDesktop("1") }}</td>
        <td>4.0</td>
        <td>4.0</td>
        <td>1.0</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>1.0</td>
        <td>{{ CompatGeckoMobile("1") }}</td>
        <td>6.0</td>
        <td>6.0</td>
        <td>1.0</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">Specification</h2>
<p>Part of DOM level 0, as specified in <a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#timers" title="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#timers">HTML5</a>.</p>
Revert to this revision