DOM:window.setInterval
From MDC
« Gecko DOM Reference
Contents |
[edit]
Summary
Calls a function repeatedly, with a fixed time delay between each call to that function.
[edit]
Syntax
ID = window.setInterval(funcName, delay)
IDis a unique interval ID you can pass to clearInterval.funcNameis the function you want to be called repeatedly.delayis the number of milliseconds (thousandths of a second) that thesetInterval()function should wait between each call tofuncName.
[edit]
Example
var intervalID = window.setInterval(animate, 500);
The following example will continue to call the flashtext() function once a second, until you clear the intervalID by clicking the Stop button.
<html>
<head>
<title>setInterval/clearInterval example</title>
<script type="text/javascript">
var intervalID;
function changeColor()
{
intervalID = setInterval(flashText, 1000);
}
function flashText()
{
var elem = document.getElementById("my_box");
if (elem.style.color == 'red')
{
elem.style.color = 'blue';
}
else
{
elem.style.color = 'red';
}
}
function stopTextColor()
{
clearInterval(intervalID);
}
</script>
</head>
<body onload="changeColor();">
<div id="my_box">
<p>Hello World</p>
</div>
<button onclick="stopTextColor();">Stop</button>
</body>
</html>
[edit]
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.
See notes at the talk page and setTimeout reference.
[edit]
Specification
DOM Level 0. Not part of any standard.
Categories: Gecko DOM Reference | DOM 0