Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 1.03 KB

File metadata and controls

37 lines (24 loc) · 1.03 KB

31. Timers and browser scheduling

Schedule UI updates without blocking the page.

Learn

  • setTimeout
  • setInterval
  • requestAnimationFrame
  • cleanup

How it works

setTimeout schedules a later callback; setInterval repeats until cleared. requestAnimationFrame schedules visual work before a repaint. Store timer IDs and clean them up when a component is removed. Long synchronous loops still block interaction while they run.

Common mistake

A timer can keep updating an element that no longer exists, and intervals can accumulate after repeated setup.

Guided challenge

Build a countdown with start and stop controls, then prevent duplicate timers.

Run the example

Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.

Completion check

  • Explain the core idea in your own words without reading the code.
  • Change one input and predict the output before running it.
  • Test one boundary or failure case, then record what you changed.