DOM:window.setInterval
From MDC
(Difference between revisions)
| Revision as of 18:16, 7 July 2006 Jabez (Talk | contribs) Example � Previous diff |
Revision as of 18:17, 7 July 2006 Jabez (Talk | contribs) Example Next diff → |
||
| Line 14: | Line 14: | ||
| intervalID = window.setInterval("animate()", 500); | intervalID = window.setInterval("animate()", 500); | ||
| - | The following example will continue to call the flashtext() function once a second, untill you clear the <code>intervalID</code> by clicking on the Stop button. | + | The following example will continue to call the flashtext() function once a second, untill you clear the <code>intervalID</code> by clicking the Stop button. |
| <pre> | <pre> | ||
Revision as of 18:17, 7 July 2006
« Gecko DOM Reference
Contents |
Summary
Calls a function repeatedly, with a fixed time delay between each call to that function.
Syntax
ID = window.setInterval(funcName, delay)
Parameters
IDis a unique interval ID.funcNameis the name of the function which you want to repeatedly call.delayis the number of milliseconds (thousandths of a second) that the setInterval() function should wait between each call tofuncName.
Example
intervalID = window.setInterval("animate()", 500);
The following example will continue to call the flashtext() function once a second, untill you clear the intervalID by clicking the Stop button.
<html>
<head>
<title>setInterval/clearInterval example</title>
<script type="text/javascript">
function changeColor()
{
intervalID = window.setInterval("flashText()", 1000);
}
function flashText()
{
elem = document.getElementById("my_box");
if (elem.style.color == 'red')
{
elem.style.color = 'blue';
}
else
{
elem.style.color = 'red';
}
}
function stopTextColor()
{
window.clearInterval(intervalID);
}
</script>
</head>
<body onload="changeColor();">
<div id="my_box">
<p>Hello World</p>
</div>
<button onclick="stopTextColor();">Stop</button>
</body>
</html>
Notes
The interval ID is used to refer to the specific interval when it needs to be cleared. The window.setInterval function is commonly used to set a delay for functions that are executed again and again, such as animations. See also window.clearInterval.
Specification
DOM Level 0. Not part of specification.
Categories: Gecko DOM Reference