Project: Countdown Timer
Use setInterval to update every second and clear it when done. Compute remaining time from a target date instead of counting down variables.
Example
const target = Date.now() + 3000;
const id = setInterval(() => {
const left = Math.max(0, Math.ceil((target - Date.now()) / 1000));
console.log("left", left);
if (left === 0) { clearInterval(id); console.log("done"); }
}, 1000);Exercise
Stop an interval.
(id)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.