Eye Training · Timers
Every timer needs three answers: what clears it, what if the work is slow, and what if it runs late.
Part 2 · Surface 13. Bug counts are hidden and some snippets are clean. See Start Here for how to use these drills.
J · The basics
Bug hunt
J · Hide the toast
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Retry messages
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Start the clock
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Toast class
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
M · Three years in, these should jump out
Bug hunt
M · Job worker
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Debounce
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Search boxes
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Quiz countdown
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Poll a job
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Remind in N days
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Per-user reminders
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Fetch with a timeout
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
S · Where seniors stand out
Bug hunt
S · Repeat until stopped
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · CLI with a spinner
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
S · Session expiry
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
What your eye checks
Whenever you see setTimeout or setInterval, ask:
- What clears it? Every interval needs a
clearIntervalon the path that ends it: unmount, close, stop, done, and failure. - What if the work takes longer than the interval?
setIntervaldoesn't wait for async work; chainedsetTimeoutdoes. - Is it passed a function, or the result of calling one?
- What does the callback capture?
varin a loop,thisin a plainfunction, stale variables. - What if it runs late? Timers are a minimum delay, not a promise. Background tabs throttle them, and busy event loops delay them. Compute from a deadline, never by counting ticks.
- Can two callers share one timer by accident, or can one caller orphan a timer by overwriting its id?
- What if the process restarts, or there are several instances? In-memory timers are lost on restart and are per-instance. Anything that must happen belongs in a database or job queue.
- Does it hold the process open? An active timer keeps Node alive; a CLI or a test needs it cleared or
unrefed.
Tells
| You see | Ask |
|---|---|
setInterval(async | What if a tick takes longer than the interval? |
setInterval( with no id saved | How is it ever stopped? |
setTimeout(fn(), …) | Is the function called now? |
for (var around a timer | Which i does the callback see? |
setTimeout(function () { this… | What is this here? |
| A counter decremented each tick | What if the tick is late? |
A module-level let timer | Who else shares it? |
timers[key] = setTimeout | What happens to the old one? Who deletes the entry? |
| A delay in days | Is it over 2³¹ ms? Does it survive a restart? |
| A polling loop | What stops it on failure? Is there a limit? |
Go deeper
- The Event Loop Under Pressure: why timers run late, with a simulation
- Controlling Concurrency: Part 10 builds debounce and throttle; Cancellation and Cleanup covers timeouts that don't cancel anything
- Previous surface: Promises and async/await · Next: Events, listeners and sockets