JavaScript provides timer functions for delayed or repeated execution of code in the browser (or event loop in Node.js).

setTimeout(fn, delay) / clearTimeout(id)

const id = setTimeout(() => {
  console.log("Executed after 1 second");
}, 1000);

clearTimeout(id); // Cancels execution

setInterval(fn, interval) / clearInterval(id)

const id = setInterval(() => {
  console.log("Runs every 1 second");
}, 1000);

clearInterval(id); // Stops further executions

requestAnimationFrame(callback)

function animate() {
  // update visual state
  requestAnimationFrame(animate); // loop
}

requestAnimationFrame(animate);