setTimeout, setInterval, and timer drift
Schedule one-off and repeating tasks with setTimeout/setInterval and learn a simple technique to reduce timer drift.
Intro to timers
Goal: Schedule work cleanly.
- setTimeout: run later (once)
- setInterval: run repeatedly
- Drift: why timers slip
- Simple drift fix

setTimeout — one-off
setTimeout(fn, ms) runs once after the delay (minimum timing, not exact).
// setTimeout schedules a one-off callback
console.log("A: start");
setTimeout(function () {
console.log("B: after ~5ms");
}, 5);
console.log("C: scheduled");

All lessons in this course
- setTimeout, setInterval, and timer drift
- Debounce vs Throttle — hand-rolled
- Animation Timing — intro with time-based loops