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

JS Puzzles · Const

setTimeout 0

setTimeout(func, 0) schedules a callback as soon as possible, but only after the current script completes. It does not mean the callback runs immediately or exactly zero milliseconds later.

How it works

Calling setTimeout with a zero delay queues its callback for the earliest possible time. The callback cannot run until the currently executing script completes; in browsers, repeated nested timers can also be subject to a minimum delay.

Syntax

setTimeout(func, delay)

Parameters

NameTypeDescription
funcfunctionThe function to schedule for later execution.
delaynumberOptional delay in milliseconds; use 0 for zero-delay scheduling.

Returns

A timer identifier that can be used to cancel the scheduled timeout.

Key points

  • The zero-delay form can be written with an explicit 0 or with the delay omitted.
  • The callback is scheduled for the earliest time the scheduler can run it.
  • Execution occurs after the current script, not in the middle of it.
  • The scheduler records the callback as ready after the nominal delay.
  • Synchronous code already in progress runs before the timeout callback.

Common mistakes

  • A zero delay does not interrupt the current script; the callback waits until that script finishes.
  • In browsers, repeated nested zero-delay timers may be forced to wait at least 4 ms.
  • The requested delay is not an exact execution-time guarantee.

Example: Run after the current script

console.log("Hello");
setTimeout(() => console.log("World"), 0);

The callback is queued for the earliest possible time, but it runs only after the current script finishes, so the output is Hello followed by World.

When to use it

Postpone follow-up work

Defer work until synchronous initialization has completed.

Keep long work responsive

Split a large calculation into smaller chunks so the browser can process other tasks between them.

Defer event-handler effects

Run an action after an event has finished bubbling.

  • setTimeout
  • setInterval
  • event loop
  • macrotask

Source: Scheduling: setTimeout and setInterval. Checked 2026-10-15.