Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Function

setInterval

setInterval repeatedly invokes a callback at a specified millisecond delay. It returns a Timeout handle that can be passed to clearInterval to stop future invocations.

Since
v0.0.1

How it works

setInterval schedules a callback to run repeatedly, waiting the specified number of milliseconds between scheduled executions. It returns a Timeout object, which can be supplied to clearInterval when the repeated work should stop; optional arguments are passed to each callback invocation.

Syntax

setInterval(callback[, delay[, ...args]])

Parameters

NameTypeDescription
callbackFunctionFunction invoked whenever the interval timer elapses.
delaynumberMilliseconds to wait before invoking the callback; defaults to 1.
...argsanyOptional values passed to the callback each time it runs.

Returns

<Timeout> for use with clearInterval().

Key points

  • It repeatedly schedules the callback at the specified millisecond delay.
  • The callback parameter must be the function that should run when each interval elapses.
  • Delay is a number expressed in milliseconds and controls the interval's timing.
  • Values after the delay are optional and are forwarded to each callback invocation.
  • The function returns a Timeout handle intended for later cancellation.
  • The API is globally available, so importing the timers module is unnecessary.

Common mistakes

  • Delays outside the supported range, including NaN, are normalized to 1 millisecond rather than preserving the requested value.
  • Keep the returned Timeout and pass it to clearInterval to cancel future executions.

Example: Stop a repeating timer

let count = 0;
const interval = setInterval(() => {
  console.log(++count);
  if (count === 3) clearInterval(interval);
}, 100);

Prints three values at roughly 100-millisecond intervals, then cancels the returned timer.

When to use it

Periodic background work

Run recurring maintenance such as polling or refreshing cached data at a fixed cadence.

Repeated work with explicit termination

Drive a bounded demonstration, retry loop, or heartbeat until a stopping condition is reached.

Callback argument delivery

Pass stable configuration values to each callback without rebuilding the timer callback's surrounding state.

Interview questions

QWhat does setInterval return, and how is it cancelled?

setInterval(callback[, delay[, ...args]]) schedules repeated callback execution every delay milliseconds and returns a Timeout usable with clearInterval. The callback receives any additional arguments supplied to setInterval.

Follow-up: How should an interval be stopped? Store its returned Timeout and call clearInterval with that object when the work is complete.

QWhat is the meaning and unit of the delay parameter?

The callback is invoked repeatedly according to the requested delay, while the timer remains scheduled. The delay is measured in milliseconds and defaults to 1 when omitted.

Follow-up: What happens for a negative delay? Node.js sets it to 1 millisecond, as it does for values below 1.

QMust setInterval be imported, and what are its main parameters?

The function is a global timer API, so it can be called without importing node:timers. Its callback parameter is the function invoked whenever the timer elapses.

Follow-up: How can callback data be provided without closing over it? Add values after delay; Node.js passes those values to every callback invocation.

  • clearInterval
  • setTimeout
  • setImmediate
  • Timeout

Source: Timers. Checked 2026-10-15.