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
-
timeoutIDis the ID of the timeout, which can be used with window.clearTimeout. -
funcis the function you want to execute afterdelaymilliseconds. -
codein the alternate syntax, is a string of code you want to execute afterdelaymilliseconds. -
delayis 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.
Categories: Gecko DOM Reference | DOM 0