window.setTimeout

  • Revision slug: DOM/window.setTimeout
  • Revision title: window.setTimeout
  • Revision id: 29783
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment

Revision Content

{{template.DomRef()}}

Summary

Executes a code snippet or a function after specified delay.

Syntax

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

where

  • timeoutID is the ID of the timeout, which can be used 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.
  • delay is the number of milliseconds (thousandths of a second) that the function call should be delayed by.

Examples

window.setTimeout('window.parent.generateOutput()', 1000);
function generateOutput(aConcise) {
  if(aConcise)
    parent.generateConciseOutput();
  else
    parent.generateOutput();
}
window.setTimeout(generateOutput, 1000, true);
<html>
<head>
<title>setTimeout example</title>

<script type="text/javascript">
function delayedAlert()
{
  timeoutID = window.setTimeout(slowAlert, 2000);
}

function slowAlert()
{
  alert("That was really slow!");
}

function clearAlert()
{
  window.clearTimeout(timeoutID);
}
</script>
</head>

<body>
<button onclick="delayedAlert();"
 >show an alert box after 2 seconds</button><br />
<button onclick="clearAlert();">Cancel</button>
</body>
</html>

See also clearTimeout() example.

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().

Specification

{{template.DOM0()}}

{{ wiki.languages( { "fr": "fr/DOM/window.setTimeout", "pl": "pl/DOM/window.setTimeout" } ) }}

Revision Source

<p>
{{template.DomRef()}}
</p>
<h3 name="Summary"> Summary </h3>
<p>Executes a code snippet or a function after specified delay.
</p>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval"><i>timeoutID</i> = window.setTimeout(<i>func</i>, <i>delay</i>[, <i>param1</i>, <i>param2</i>, ...]);
<i>timeoutID</i> = window.setTimeout(<i>code</i>, <i>delay</i>);
</pre>
<p>where
</p>
<ul><li> <code>timeoutID</code> is the ID of the timeout, which can be used with <a href="en/Window.clearTimeout">window.clearTimeout</a>.
</li><li> <code>func</code> is the <a href="en/Core_JavaScript_1.5_Reference/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.
</li><li> <code>delay</code> is the number of milliseconds (thousandths of a second) that the function call should be delayed by.
</li></ul>
<h3 name="Examples"> Examples </h3>
<pre class="eval">window.setTimeout('window.parent.generateOutput()', 1000);
</pre>
<pre class="eval">function generateOutput(aConcise) {
  if(aConcise)
    parent.generateConciseOutput();
  else
    parent.generateOutput();
}
window.setTimeout(generateOutput, 1000, true);
</pre>
<pre>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;setTimeout example&lt;/title&gt;

&lt;script type="text/javascript"&gt;
function delayedAlert()
{
  timeoutID = window.setTimeout(slowAlert, 2000);
}

function slowAlert()
{
  alert("That was really slow!");
}

function clearAlert()
{
  window.clearTimeout(timeoutID);
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;button onclick="delayedAlert();"
 &gt;show an alert box after 2 seconds&lt;/button&gt;&lt;br /&gt;
&lt;button onclick="clearAlert();"&gt;Cancel&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>See also <a href="en/DOM/window.clearTimeout#Example">clearTimeout() example</a>.
</p>
<h3 name="Notes"> Notes </h3>
<p>You can cancel the timeout using <a href="en/DOM/window.clearTimeout">window.clearTimeout()</a>.
</p><p>If you wish to have your function called repeatedly (i.e. every N milliseconds), consider using <a href="en/DOM/window.setInterval">window.setInterval()</a>.
</p>
<h3 name="Specification"> Specification </h3>
<p>{{template.DOM0()}}
</p>{{ wiki.languages( { "fr": "fr/DOM/window.setTimeout", "pl": "pl/DOM/window.setTimeout" } ) }}
Revert to this revision