Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 18 / 34

Timers & their real behavior

01

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.

Scheduling work and measuring time
ToolBehaviorGood use
setTimeoutMakes one callback eligible after a delayA one-off reminder or delayed retry
setIntervalRepeatedly makes the callback eligiblePeriodic work that has clear cleanup
clearTimeout / clearIntervalCancel a saved timer handleLeaving a screen or replacing pending work
performance.now()Measures elapsed time without wall-clock adjustmentsChecking duration without wall-clock changes
Date.now()Reads wall-clock time in millisecondsComparing 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.

02

Mental model

FOLLOW THE VALUE

schedule timer
delay threshold
eligible callback

A timer asks for future work.

01 / 03Trace the transformation

03

Live example

The cancelled produces no output. The remaining zero-delay callback still runs after end.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Cancel the intended timer

Pass the timer’s actual handle to clearTimeout so only later runs.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

start
end
later

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does a timer delay guarantee?
    02Does setInterval wait for an async callback to finish?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson