Login required to edit - Edit - MDC, , HTML - MDC, Download Mozilla Source Code - MDC, Login required to edit - Edit - MDC, Accessible XUL Authoring Guidelines - MDC, Login required to edit - Edit - MDC, RDF - MDC, Gecko DOM Reference:Introduction - MDC, Login required to edit - Edit - MDC

DOM:window.setTimeout

From MDC

« Gecko DOM Reference

Contents

[edit]

Summary

Executes a code snippet or a function after specified delay.

[edit]

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.
[edit]

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.

[edit]

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

[edit]

Specification

DOM Level 0. Not part of any standard.

Retrieved from "http://developer.mozilla.org/en/docs/DOM:window.setTimeout"