0Pricing
JavaScript Academy · Lesson

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, setInterval, and timer drift — illustration 1

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");
setTimeout, setInterval, and timer drift — illustration 2

All lessons in this course

  1. setTimeout, setInterval, and timer drift
  2. Debounce vs Throttle — hand-rolled
  3. Animation Timing — intro with time-based loops
← Back to JavaScript Academy