The Wayback Machine - https://web.archive.org/web/20080902061533/http://developer.mozilla.org:80/en/DOM/window.setTimeout

Mozilla.com

  1. MDC
  2. Main Page
  3. DOM
  4. window.setTimeout

Some features of this site require JavaScript.

window.setTimeout

« Gecko DOM Reference

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. (not recommended)
  • delay is the number of milliseconds (thousandths of a second) that the function call should be delayed by.

Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer.

Compatibility

Introduced with JavaScript 1.0, Netscape 2.0. Passing a Function object reference was introduced with JavaScript 1.2, Netscape 4.0; supported by the MSHTML DOM since version 5.0.

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

The 'this' problem

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. This issue is explained in more detail in the JavaScript reference.

Specification

DOM Level 0. Not part of any standard.

Page last modified 12:36, 2 Jul 2008 by Vor0nwe?

Files (0)

 
 
 
Morty Proxy This is a proxified and sanitized view of the page, visit original site.