setInterval()

Repeatedly calls a function with a fixed delay between each call.

Since ES1 Spec ↗

Syntax

setInterval(callback, delay, ...args)

Returns

number — A timer id that can be passed to `clearInterval()`.

Examples

let n = 0;
const id = setInterval(() => {
  n++;
  console.log(n);
  if (n === 3) clearInterval(id);
}, 10);
Output
1
2
3

Notes

- Always clear the interval with `clearInterval()` to stop it. - If callbacks take longer than the interval, they may run back to back without idle time. - For animations prefer `requestAnimationFrame()`.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also