The event loop lets Node.js do many things at once by managing tasks in steps called phases. Timers run in a special phase to schedule code after a delay.
Event loop phases and timer execution in Node.js
setTimeout(callback, delay) setInterval(callback, delay)
setTimeout runs the callback once after the delay (in milliseconds).
setInterval runs the callback repeatedly every delay milliseconds.
setTimeout(() => {
console.log('Hello after 1 second');
}, 1000);setInterval(() => {
console.log('Repeats every 2 seconds');
}, 2000);console.log('Start'); setTimeout(() => { console.log('Timeout'); }, 0); console.log('End');
This program shows how timer callbacks run after the current code finishes, in the timers phase of the event loop. The shorter delay callback runs first.
console.log('Phase 1: Start'); setTimeout(() => { console.log('Timer callback runs in timers phase'); }, 100); setTimeout(() => { console.log('Another timer callback'); }, 50); console.log('Phase 1: End');
Timers run in the timers phase of the event loop, after the current code completes.
Even a 0 ms delay means the callback waits until the timers phase, so it runs after synchronous code.
Callbacks with shorter delays usually run before longer delays, but exact timing depends on system and event loop state.
The event loop has phases; timers run in the timers phase.
setTimeout schedules code to run after a delay in the timers phase.
Even zero delay callbacks run after current synchronous code finishes.