Postpone follow-up work
Defer work until synchronous initialization has completed.
JS Puzzles · Const
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.
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.
setTimeout(func, delay)| Name | Type | Description |
|---|---|---|
func | function | The function to schedule for later execution. |
delay | number | Optional delay in milliseconds; use 0 for zero-delay scheduling. |
A timer identifier that can be used to cancel the scheduled timeout.
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.
Defer work until synchronous initialization has completed.
Split a large calculation into smaller chunks so the browser can process other tasks between them.
Run an action after an event has finished bubbling.
setTimeoutsetIntervalevent loopmacrotask