8 min read

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:

  1. What clears it? Every interval needs a clearInterval on the path that ends it: unmount, close, stop, done, and failure.
  2. What if the work takes longer than the interval? setInterval doesn't wait for async work; chained setTimeout does.
  3. Is it passed a function, or the result of calling one?
  4. What does the callback capture? var in a loop, this in a plain function, stale variables.
  5. 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.
  6. Can two callers share one timer by accident, or can one caller orphan a timer by overwriting its id?
  7. 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.
  8. Does it hold the process open? An active timer keeps Node alive; a CLI or a test needs it cleared or unrefed.

Tells

You seeAsk
setInterval(asyncWhat if a tick takes longer than the interval?
setInterval( with no id savedHow is it ever stopped?
setTimeout(fn(), …)Is the function called now?
for (var around a timerWhich i does the callback see?
setTimeout(function () { this…What is this here?
A counter decremented each tickWhat if the tick is late?
A module-level let timerWho else shares it?
timers[key] = setTimeoutWhat happens to the old one? Who deletes the entry?
A delay in daysIs it over 2³¹ ms? Does it survive a restart?
A polling loopWhat stops it on failure? Is there a limit?

Go deeper