Timers

setTimeout

A timeout runs a callback once after a millisecond delay. Use it for retries, debounced saves, and friendly startup messages.

const timer = setTimeout(() => { console.log("tick"); }, 500);

tick

The delay is a minimum, not a promise of exact timing. Busy work can push the callback later, never earlier.

setInterval

An interval repeats the same callback on a steady cadence. It fits clocks, polls, and heartbeat logs.

let n: number = 0; const clock = setInterval(() => { n += 1; console.log("beat " + n); if (n === 3) { clearInterval(clock); } }, 10);

beat 1
beat 2
beat 3

Clear them

Every timer returns a handle, and every handle needs a matching clear call. Clearing stops future runs and lets short scripts exit.

const once = setTimeout(() => { console.log("never"); }, 1000); const tick = setInterval(() => { console.log("beat"); }, 1000); clearTimeout(once); clearInterval(tick);

Cancel as soon as the work is done or no longer needed. Explicit cleanup beats hoping the process ends on its own.

unref in servers

A pending timer keeps Node alive by default, which can stall shutdown. The unref call marks housekeeping timers as non-blocking for exit.

const beat = setInterval(() => { console.log("housekeeping"); }, 60000); beat.unref();

Use unref for background cleanup only, never for work users wait for. Main responses stay referenced so they always complete.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does setTimeout return?

    Answer

    A handle that can clear the timer

    The handle is what clearTimeout takes to cancel the scheduled run.

  2. Is the delay of setTimeout exact?

    Answer

    No: it is a minimum, and busy work can push the callback later

    The event loop runs the callback when it can, never before the delay.

  3. Why clear a timer that is no longer needed?

    Answer

    It stops future runs and lets a short script exit

    A pending timer keeps Node alive, and clearing removes that work.

  4. What does unref() do to a timer?

    Answer

    It marks the timer as non-blocking for process exit

    Housekeeping timers can stop holding the process open, while user work stays referenced.

  5. What does this program print? setTimeout(() => console.log("later"), 0); console.log("now");

    Answer

    now / later

    The synchronous log runs first, and the zero-delay callback runs after it.

  6. What does this program print? setTimeout(() => console.log("tick"), 10); console.log("start");

    Answer

    start / tick

    The timer callback waits for the current code to finish, so start comes first.

  7. What does this program print? const t = setTimeout(() => console.log("never"), 10); clearTimeout(t); console.log("done");

    Answer

    done

    The timer is cleared before it fires, so only the synchronous log prints.

  8. What does this program print? let n = 0; const id = setInterval(() => { n += 1; console.log(n); if (n === 2) { clearInterval(id); } }, 10);

    Answer

    1 / 2

    The interval prints twice and clears itself on the second run.

  9. What does the compiler say about this program? const t: number = setTimeout(() => {}, 10); console.log(t);

    Answer

    error TS2322: Type 'Timeout' is not assignable to type 'number'.

    The Node timer handle is a Timeout object, not a number.

  10. What does the compiler say about this program? clearTimeout(); console.log("done");

    Answer

    error TS2554: Expected 1 arguments, but got 0.

    The clear call needs the handle it should cancel.

Article author: Arthur Isaev