Themes - MDC, Login required to edit - Edit - MDC, Mozilla Embedding FAQ - MDC, Login required to edit - Edit - MDC, Category:XHTML - MDC, Categories - MDC, XUL:Attribute:menuitem.key - MDC, Interfaces:About Frozen Interfaces - MDC, Special pages - MDC, Categories - MDC

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) 
  • ID is a unique interval ID you can pass to clearInterval.
  • funcName is the function you want to be called repeatedly.
  • delay is the number of milliseconds (thousandths of a second) that the setInterval() function should wait between each call to funcName.
[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.

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