Complete the code to run a function every 2 seconds using setInterval.
const intervalId = setInterval([1], 2000);
The setInterval function expects a function as its first argument. Using an arrow function () => console.log('Hello') correctly passes a function to run every 2 seconds.
Complete the code to stop the interval after 5 seconds using clearInterval.
const intervalId = setInterval(() => console.log('Tick'), 1000); setTimeout(() => [1](intervalId), 5000);
To stop a repeating interval, use clearInterval with the interval ID.
Fix the error in the code to correctly stop the interval after 3 seconds.
const id = setInterval(() => console.log('Running'), 1000); setTimeout(() => clearInterval([1]), 3000);
The variable id holds the interval ID. Pass it to clearInterval to correctly stop the interval after 3 seconds.
Fill both blanks to create an interval that logs 'Hi' every second and stops after 4 seconds.
const [1] = setInterval(() => console.log('Hi'), 1000); setTimeout(() => [2]([1]), 4000);
We store the interval ID in intervalId. To stop it, we call clearInterval(intervalId).
Fill all three blanks to create an interval that counts seconds and stops after 3 counts.
let count = 0; const [1] = setInterval(() => { [2]; console.log(count); if (count === 3) { [3]([1]); } }, 1000);
We store the interval ID in intervalId. We increment count with count++. When count reaches 3, we stop the interval with clearInterval(intervalId).