setInterval()
noun
say it: “set IN-ter-vul” /sɛt ˈɪntərvəl/
A JavaScript function that runs another function again and again, waiting the number of milliseconds you give it between runs, until you stop it.
What is setInterval() in JavaScript?
setInterval() — Runs a function again and again, once every interval you give in milliseconds, until clearInterval() stops it with the ID it returned. It's what makes a countdown or a clock tick.
let secondsLeft = 3;
const timerId = setInterval(() => {
console.log(secondsLeft); // 3, then 2, then 1
secondsLeft--;
if (secondsLeft === 0) {
clearInterval(timerId);
}
}, 1000);
In a sentence
- “The workout timer calls setInterval() with 1000, so the countdown drops by one every second.”
- “I used setInterval() to show a new photo of an adoptable pet every five seconds.”
Level: Apprentice · More JavaScript terms at this level
Updated