DOM Window.setInterval() Method
The setInterval() method runs a function repeatedly, with a fixed delay between each run, until it is stopped with clearInterval().
Description
The setInterval() method calls a function repeatedly, waiting the given delay between calls, until clearInterval() is called with its ID. Browsers slow intervals down in background tabs. If each run might take longer than the delay, or you need a varying delay, a chain of setTimeout() calls is more reliable. For animation, use requestAnimationFrame().
The callback is first called after one delay has passed, not immediately; call your function once yourself before starting the interval if it should run straight away. Intervals keep running until cleared, even if the elements they update have been removed, so always keep the ID and clear it when finished.
Browsers enforce a minimum delay of a few milliseconds, and nested or background-tab timers can be slowed to once per second or less to save power, so intervals aren't suitable for precise timing. For clocks, base the display on Date.now() rather than counting ticks.
Syntax
setInterval(callback, delay)
setInterval(callback, delay, arg1, arg2, ...)Parameters
- callbackThe function to run repeatedly.
- delayThe time between runs, in milliseconds.
- arg1, arg2, ...Optional. Arguments passed to the callback.
Return Value
A positive integer interval ID, which can be passed to clearInterval().
Example
Browser Support
The following information will show you the current browser support for the Window.setInterval() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





