A timer never runs code at a time; it queues a task. The HTML Standard's run steps after a timeout waits at least the delay, optionally longer, and then queues the callback on the timer task source. The callback still has to wait for:
- The running task and its microtasks. A 10 ms timer set before 200 ms of synchronous work runs after 200 ms.
- Other queued tasks and rendering. The browser chooses which task queue to serve next, and timers aren't top priority.
- The nesting clamp. After five nested levels, delays under 4 ms become 4 ms (see the JavaScript async chapter).
- Power-saving padding. The spec lets browsers wait "a further implementation-defined length of time". With a headless page hidden behind another tab, Chrome 156 and Firefox 159 both ran a 100 ms interval once per second. Chrome goes further: after a page has been hidden for 5 minutes, chained timers wake at most once a minute (intensive throttling, since Chrome 88).
Two more traps:
- The delay is a Web IDL
long. Values are converted modulo 2³², so anything above 2,147,483,647 ms (about 24.8 days) wraps around, usually to a negative number, which is treated as 0. A 30-day timeout fires immediately;Infinitybecomes 0 too. setIntervaldoesn't catch up. If the main thread is blocked through several periods, the callback runs once when it's free, not once per missed period. The spec re-arms the interval after each callback, but neither engine follows that exactly: a 50 ms callback on a 100 ms interval still started every 100 ms in Chrome 156 and Firefox 159.
So use timers for "not before", never for "exactly at". For visual updates use requestAnimationFrame; for yielding, scheduler.yield().