References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript setInterval() method

Method JavaScript All modern browsers Updated
Quick answer

The setInterval() method runs a function over and over at a fixed delay in milliseconds. setInterval(tick, 1000) runs tick every second. It returns an id you pass to clearInterval() to stop it. Always clear it when you're done, or it runs forever and leaks.

Overview

setInterval() is the repeating cousin of setTimeout(): instead of running a function once, it runs it again and again, every N milliseconds, until you stop it. Clocks, polling for updates, animations, slideshows — anything that ticks on a schedule.

It returns a timer id, and you must hold onto it, because clearInterval(id) is the only way to stop the loop. An interval that's never cleared keeps firing forever — even after the user navigates away from the part of the UI that needed it — which wastes resources and is a classic memory-leak source. Clear it as soon as the work is done or the component goes away.

A couple of caveats: like setTimeout(), the interval is a minimum, and if your callback takes longer than the interval, calls can bunch up. For precise or drift-free timing (and for animation specifically), requestAnimationFrame() or a self-scheduling setTimeout() chain can be better. But for straightforward "do this every second" jobs, setInterval() is exactly right.

Syntax

const id = setInterval(callback, delay)

const id = setInterval(() => console.log("tick"), 1000);
clearInterval(id);  // stop it

Parameters

The setInterval() method accepts the following parameters.

Parameter Description
callback The function to run on each interval.
delay The interval between runs, in milliseconds.
arg1 ... argN Optional. Extra arguments passed to the callback each time.

Example

Live example
<p id="clock" style="font:18px ui-monospace,monospace"></p>
<script>
  const clock = document.getElementById('clock');

  function tick() {
    clock.textContent = new Date().toLocaleTimeString();
  }
  tick();
  const id = setInterval(tick, 1000);
  // call clearInterval(id) to stop
</script>

Best practices

  • Always keep the id and call clearInterval(id) when finished — uncleared intervals run forever and leak.
  • Stop the interval when its UI is removed or hidden, not just when the page unloads.
  • For animation, prefer requestAnimationFrame(), which syncs to the display refresh.
  • If the callback can outlast the interval, consider a self-scheduling setTimeout() chain to avoid pile-ups.

Frequently asked questions

How do I run code repeatedly in JavaScript?
Use setInterval(callback, delay) with the delay in milliseconds. It runs the callback over and over until cleared.
How do I stop a setInterval()?
Save the id it returns and call clearInterval(id).
What is the difference between setTimeout() and setInterval()?
setTimeout() runs once; setInterval() runs repeatedly at a fixed interval until you clear it.
Why is my setInterval() causing problems?
Likely it's never cleared, so it keeps running and stacking up. Always clearInterval() when the work is done.