Recall & Review
beginner
What does
setInterval do in Node.js?setInterval runs a function repeatedly at a fixed time interval until stopped.
Click to reveal answer
beginner
How do you stop a repeating action started by
setInterval?You use clearInterval with the interval ID returned by setInterval.
Click to reveal answer
beginner
What type of value does
setInterval return?It returns an interval ID (a number or object) that identifies the timer.
Click to reveal answer
intermediate
Why should you always clear intervals when they are no longer needed?
To avoid memory leaks and unnecessary CPU usage by stopping the repeated function calls.
Click to reveal answer
beginner
Write a simple example of
setInterval and clearInterval usage.<pre>const id = setInterval(() => {
console.log('Hello every second');
}, 1000);
setTimeout(() => {
clearInterval(id);
console.log('Stopped interval');
}, 5000);</pre>Click to reveal answer
What does
setInterval return when called?✗ Incorrect
setInterval returns an ID that you use with clearInterval to stop the repeated calls.
How do you stop a function from running repeatedly after using
setInterval?✗ Incorrect
clearInterval stops the repeated execution started by setInterval.
What happens if you never call
clearInterval on a running interval?✗ Incorrect
The function will keep running repeatedly until you clear the interval or the program ends.
Which of these is a correct way to use
setInterval?✗ Incorrect
Passing a function reference or arrow function is correct. Option B calls the function immediately, not repeatedly.
What unit is the delay time in
setInterval?✗ Incorrect
The delay time is always in milliseconds (thousandths of a second).
Explain how
setInterval and clearInterval work together in Node.js.Think about starting and stopping a repeating alarm.
You got /3 concepts.
Describe why it is important to clear intervals when they are no longer needed.
Imagine leaving a timer running that you don’t need anymore.
You got /3 concepts.