JavaScript Window setInterval() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.setInterval() repeatedly calls a function with a fixed delay between each run (MDN). It returns an interval ID — pass that ID to clearInterval() to stop. For a one-shot delay, use setTimeout().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.setInterval. Start with this line.

JavaScript
setInterval(fn, delay, ...args)
CallWhat it does
setInterval(fn, delay, ...args)Primary signature.
window.setInterval(...)Call with the arguments your case needs.

Built into the browser. Call window.setInterval(...) (or the global setInterval(...)) — no import required.

Library declarations
JavaScript
setInterval(fn, delay, ...args)

Parameters

ParameterDescription
funcRequired. Function to run each tick
delayOptional. Milliseconds between runs (default 0)
param1…NOptional. Forwarded to func each tick

Return Value

Return ValueDescription
*Pass to clearInterval()

JavaScript Examples

Example 1 — Basic Repeating Call

MDN: schedule a callback every fixed delay.

JavaScript
const id = setInterval(function () {
  console.log("tick");
}, 1000);

// later: clearInterval(id);
Try It Yourself

How It Works

  1. Schedule. setInterval registers the callback.
  2. Delay. First run is after 1000 ms, then every 1000 ms.
  3. ID. Store the return value to stop later.
  4. Cleanup. Call clearInterval(id) when done.

Example 2 — Pass Extra Arguments

MDN: arguments after delay are forwarded to the callback.

JavaScript
function greet(name) {
  console.log("Hello, " + name);
}

const id = setInterval(greet, 2000, "Alex");
// later: clearInterval(id);
Try It Yourself

How It Works

  1. Callback. greet expects a name.
  2. Args after delay. "Alex" is passed each tick.
  3. Alternative. setInterval(() => greet("Alex"), 2000).
  4. Stop. Still use clearInterval(id).

Example 3 — Stop with clearInterval

MDN: cancel the repeating timer with the stored ID.

JavaScript
let n = 0;
const id = setInterval(function () {
  n += 1;
  console.log(n);
  if (n >= 3) {
    clearInterval(id);
    console.log("stopped");
  }
}, 500);
Try It Yourself

How It Works

  1. Count ticks. Increment on each run.
  2. Stop condition. After 3 ticks, clear the ID.
  3. No more runs. Callback will not fire again.
  4. Pattern. Same idea as a countdown that ends.

Example 4 — Start / Stop Buttons

MDN-style: guard against starting twice; clear on stop.

JavaScript
let id = null;
let count = 0;

function start() {
  if (id !== null) return;
  id = setInterval(function () {
    count += 1;
    console.log(count);
  }, 1000);
}

function stop() {
  clearInterval(id);
  id = null;
}
Try It Yourself

How It Works

  1. Guard. Skip start if already running.
  2. Store ID. Keep id for later clear.
  3. Stop. clearInterval then set id = null.
  4. Restart. A new setInterval creates a new ID.

Example 5 — setInterval vs setTimeout

One repeats; the other runs once. MDN recommends recursive setTimeout when work may overrun the delay.

JavaScript
// repeats until cleared
const i = setInterval(() => console.log("interval"), 1000);

// runs once
setTimeout(() => console.log("timeout once"), 1000);

// later:
clearInterval(i);
Try It Yourself

How It Works

  1. Interval. Keeps logging until cleared.
  2. Timeout. Logs once after 1 second.
  3. Choose. Clocks → interval; “do this later” → timeout.
  4. Overrun tip. If work can take longer than the delay, chain setTimeout after finish.

Browser Support

Window.setInterval() is marked Baseline Widely available on MDN (since July 2015). It is not Deprecated, Experimental, or Non-standard. Available in browsers and workers; prefer function callbacks over string code.

✓ Baseline · Widely available

setInterval()

Repeat a callback on a fixed delay across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Universal timer API. Always store the ID and clear when finished; avoid string code.

Frequently Asked Questions

It repeatedly calls a function (or code) with a fixed delay between each call (MDN). The first run happens after the delay. Store the returned interval ID so you can stop it with clearInterval().
No. MDN marks Window.setInterval() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Passing a string as code is strongly discouraged (injection / XSS risk).
A positive integer interval ID. Pass that ID to clearInterval() to stop the repeating callback.
setTimeout runs once after a delay. setInterval keeps repeating until cleared. For one-shot delays use setTimeout; for clocks and polling use setInterval (or recursive setTimeout if work can overrun the delay).
Yes. Extra arguments after the delay are forwarded: setInterval(greet, 1000, "Alex"). Or wrap the call: setInterval(() => greet("Alex"), 1000).
Intervals keep firing until cleared or the page unloads. Clear when the UI no longer needs updates to save CPU and battery.

Next: setTimeout()

Run a function once after a delay.

setTimeout() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful