Timers & their real behavior
Understand the idea
A zero-delay timer runs later than expected, and a countdown drifts on a background tab. Timer delays are scheduling thresholds, not deadlines.
Eligibility is not an appointment
Timers let a reminder, retry, or small delay happen without keeping a loop busy while you wait. Waiting in a loop would occupy JavaScript's current thread and stop the page from responding. Scheduling a callback lets the current work finish instead.
One-shot and repeating timers
setTimeout schedules one callback. setInterval repeatedly makes a callback eligible until cleared. Both return a handle you can use to cancel future scheduled work. Calling the while registering it passes its result instead of a callback, so leave execution to the timer.
setTimeout schedules one callback; setInterval keeps making repeated callbacks eligible until you cancel it. Both return a handle you can save for cancellation. Pass a function to run later, not the result of calling that function now.
| Tool | Behavior | Good use |
|---|---|---|
| setTimeout | Makes one callback eligible after a delay | A one-off reminder or delayed retry |
| setInterval | Repeatedly makes the callback eligible | Periodic work that has clear cleanup |
| clearTimeout / clearInterval | Cancel a saved timer handle | Leaving a screen or replacing pending work |
| performance.now() | Measures elapsed time without wall-clock adjustments | Checking duration without wall-clock changes |
| Date.now() | Reads wall-clock time in milliseconds | Comparing against a real-world timestamp |
Why zero still runs later
A callback still waits for the current JavaScript work to finish. A busy main thread can delay it, and browsers throttle timers in background tabs. Even a delay of zero does not interrupt code. Timer order is part of the event loop, not a of wall-clock precision.
Cancel work you no longer need
An interval does not wait for an async callback's promise before scheduling another tick. Slow requests can overlap. When the next run should wait until the previous job finishes, schedule the next timeout after that job settles instead.
Measure elapsed time honestly
For a countdown, compare the current time with a recorded target time rather than subtracting one on every tick. The ticks can update the display while elapsed time remains the source of truth. Clear timers when the feature is no longer active so callbacks do not keep using stale state.
A timer can run late because other JavaScript is still running, the page is in the background, or the browser is conserving resources. For a countdown, store a target time and calculate the remaining time from the clock. Subtracting one on every interval assumes every tick arrived on schedule, which is exactly what timers do not promise.
Mental model
FOLLOW THE VALUE
A timer asks for future work.
01 / 03Trace the transformation
Live example
The cancelled produces no output. The remaining zero-delay callback still runs after end.
Your console output appears here.
Guided challenge
Cancel the intended timer
Pass the timer’s actual handle to clearTimeout so only later runs.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
start end later
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.