Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScript timers schedule callbacks for a later opportunity to run; they do not pause the program, create a parallel JavaScript thread, or guarantee an exact execution time. setTimeout requests a one-time callback, while setInterval requests recurring callbacks. In both browsers and Node.js, the callback becomes eligible after the requested delay and runs only when the host runtime can process it.

How do JavaScript timers work?

A timer is a request to the host environment—the browser or Node.js—to make a callback eligible after a delay. JavaScript continues executing synchronous code while the host tracks that delay. When it has elapsed, the callback waits until the runtime can run more work.

  1. Schedule: Code calls setTimeout or setInterval with a callback and delay.
  2. Continue: The current JavaScript work keeps running; the timer does not interrupt it.
  3. Wait: The host tracks the delay. Once it has elapsed, the callback becomes eligible to run.
  4. Run: The event loop invokes the callback when the runtime is available.

This means a delay is a minimum wait request, not a wall-clock appointment. MDN explains browser timer behavior in its Window.setTimeout documentation; Node.js describes its timer API as operating around the Node event loop in its Timers documentation.

Does setTimeout(..., 0) run immediately?

No. A zero delay schedules the callback for a later event cycle; the current synchronous code runs first. For example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
console.log("first");
setTimeout(() => console.log("timer"), 0);
console.log("second");

The synchronous messages appear before the timer callback: first, then second, then timer. The exact time at which the callback runs depends on when the runtime can process it.

Why is my setTimeout late?

A callback can run later than its requested delay when JavaScript or other runtime work keeps the event loop busy. Timers do not preempt an executing function, so a long-running callback or other work can postpone the timer callback.

Browsers also apply timer rules and scheduling policies:

  • Nested timers: After five nested timer calls, the browser enforces a minimum delay of 4 ms. This behavior follows the HTML timer rules and is described by MDN.
  • Inactive tabs: Browsers may delay timers further in inactive tabs. The policies differ, so there is no single background-tab delay that applies to every browser.
  • Long delay values: The browser converts the delay to a signed 32-bit integer. The maximum is 2,147,483,647 ms—about 24.8 days. Larger values can overflow and lead to surprising timing.

In Node.js, callback timing and ordering are not guaranteed either. Delays less than 1 ms, greater than 2,147,483,647 ms, or equal to NaN are set to 1 ms; fractional delays are truncated, according to the Node.js Timers documentation for v26.10.0.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What is the difference between setTimeout and setInterval?

API What it requests How to cancel Useful when
setTimeout(callback, delay) One callback after the delay has elapsed and the runtime can run it. clearTimeout(timerId) You want one delayed action.
setInterval(callback, delay) Recurring callbacks at the requested interval, subject to runtime scheduling. clearInterval(timerId) You want repeated work on a continuing schedule.

For recurring work where the next wait should begin only after the current operation finishes, use recursive setTimeout. It schedules the next iteration after the current callback has completed, rather than setting up recurring callbacks independently of that completion.

function runAgain() {
  doWork();
  setTimeout(runAgain, delay);
}

setTimeout(runAgain, delay);

Replace doWork and delay with the operation and wait appropriate to your program.

How browser and Node.js timers differ

Behavior Browser Node.js
Delay limits and conversion Delay is converted to a signed 32-bit integer; maximum is 2,147,483,647 ms (about 24.8 days), per MDN. Delays below 1, above 2,147,483,647, or NaN become 1 ms; fractional values are truncated, per Node.js v26.10.0 documentation.
Nested timers and inactive tabs Minimum 4 ms after five nested timer calls; inactive-tab delays vary by browser. Browser inactive-tab throttling does not apply as a browser-tab policy. Node.js documents event-loop scheduling rather than a corresponding browser-tab rule.
Return value The timer call returns an identifier that can be passed to its clear function. Timer calls return Timeout objects, usable with clearTimeout or clearInterval.
Cancellation Use clearTimeout or clearInterval for the corresponding timer. Use clearTimeout or clearInterval; promise-based timer APIs can also be canceled with an AbortSignal.
Does an active timer keep the runtime alive? Not applicable as a Node.js process-exit setting. Yes, by default. Calling timeout.unref() allows the process to exit if that timer is the only remaining activity.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do Node.js timers keep the process running?

Yes. An active Node.js timer keeps the event loop running by default, so the process does not exit while that timer remains the only pending activity. If the timer should not prevent process exit, call unref() on its returned Timeout object:

const timer = setTimeout(() => {
  console.log("timer callback");
}, 5000);

timer.unref();

With unref(), Node.js may exit before the callback runs if no other activity remains. For Node.js promise-based timer APIs, pass an AbortSignal to cancel pending work.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.