Timers in javascript
Timers let you schedule code to run later. setTimeout runs a callback once after a delay; setInterval runs it repeatedly. Both return an ID you can pass to clearTimeout or clearInterval to cancel. Timers are queued by the event loop, so delays are approximate, not exact.
Example
const id = setTimeout(() => {
console.log("Runs after 1 second");
}, 1000);
// Cancel before it fires
clearTimeout(id);
let count = 0;
const intervalId = setInterval(() => {
console.log("Tick", ++count);
if (count === 3) clearInterval(intervalId);
}, 500);setTimeout schedules a one‑off callback, while setInterval repeats until you cancel it; in both cases, the work is queued by the event loop.