Skip to the workspace
Pebellslearn by building
Asynchronous JavaScript  ›  Lesson

Timers: Schedule, Cancel and Clean Up

setTimeout 25 min

javascript / Timers: Schedule, Cancel and Clean Up

Explore each scene at your own pace.

01 · Timers: Schedule, Cancel and Clean Up

Timers: Schedule, Cancel and Clean Up

Timers: Schedule, Cancel and Clean Up

A timer schedules a callback through the host environment. Its delay is a minimum scheduling threshold, not a guarantee of exact execution time. Retain the returned handle to cancel work that is no longer needed.

An alarm requests a future turn. Keep its handle so the component that created it can also stop it.

Scheduling is not waiting inside the current function

Calling setTimeout registers future work and returns a handle. It does not pause the current call stack. A zero-delay callback still waits for the current work and relevant queued work to finish. Browser timer throttling and other tasks can make the observed delay longer. Node and browsers expose similar timer names but have different host details and handle types; do not hardcode that every handle is a number.

Walk through the modelTimers: Schedule, Cancel and Clean UpAuthored snapshots of the example; use Run below to test the code.
ownerinterval handle H
count0
Registration returns before the first future callback runs.
1 of 3

Own a timer for its entire lifetime

html

Start creates one interval. Clicking Start again first clears the previous interval, so repeated starts cannot silently multiply the work. Stop clears the current handle and replaces it with null. The callback increments a count; it does not measure elapsed seconds. A slow or backgrounded tab can skip the timing expectations implied by a label such as 'seconds elapsed'. For elapsed time, compare timestamps from an appropriate clock.

A timeout is suitable for one delayed action. An interval requests repeated callbacks, but it does not wait for a promise returned by an async callback. An async fetch in an interval can therefore overlap earlier requests. If each operation must finish before the next begins, schedule the next timeout after the current operation settles, and check a stopped flag before scheduling again.

Debounce is an ownership rule

A search field can cancel its previous timeout and schedule a new one whenever input changes. That prevents a request for every keystroke, but cancelling a timeout does not cancel a request that already started. In-flight work needs its own cancellation or stale-result policy. If the user leaves a page or destroys a widget, cancel owned timers and remove owned listeners during cleanup.

Predict, change and check

Predict the count after starting twice and stopping once. The correct claim is not an exact count after exactly one wall-clock second: it is that only one timer is owned, and no further increments occur after Stop. Record the visible count, wait, and check that it remains unchanged. Then deliberately remove the clear-before-start line to observe why ownership matters; restore it afterward.

The graded task exercises real timer scheduling and returns a count. Passing its value tests does not independently prove that every resource was cleaned up; inspect the clearInterval path and use the visible Stop experiment to check lifetime behavior.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.