0
0
Node.jsframework~5 mins

setTimeout and clearTimeout in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does setTimeout do in Node.js?

setTimeout schedules a function to run once after a specified delay in milliseconds.

Click to reveal answer
beginner
How do you stop a scheduled setTimeout before it runs?

You use clearTimeout with the timer ID returned by setTimeout to cancel the scheduled function.

Click to reveal answer
intermediate
What type of value does setTimeout return in Node.js?

It returns a Timeout object that represents the timer. This object is used to clear the timer if needed.

Click to reveal answer
intermediate
Why might you want to use clearTimeout in your code?

To prevent a scheduled function from running if the condition changes or the task is no longer needed, saving resources and avoiding unwanted actions.

Click to reveal answer
advanced
What happens if you call clearTimeout with an invalid or already cleared timer ID?

Nothing happens; clearTimeout safely ignores invalid or already cleared timers without errors.

Click to reveal answer
What does setTimeout(() => console.log('Hi'), 1000) do?
APrints 'Hi' after 1 second
BNever prints 'Hi'
CPrints 'Hi' every second
DPrints 'Hi' immediately
Which function cancels a scheduled setTimeout?
AremoveTimeout()
BcancelTimeout()
CstopTimeout()
DclearTimeout()
What do you need to keep to cancel a timeout later?
AThe function passed to <code>setTimeout</code>
BThe timer ID returned by <code>setTimeout</code>
CThe delay time in milliseconds
DNothing, you cannot cancel a timeout
If you call clearTimeout twice on the same timer ID, what happens?
AThe timer is cleared once, second call does nothing
BAn error is thrown
CThe timer restarts
DThe timer runs immediately
Which of these is true about setTimeout in Node.js?
AIt runs the function repeatedly every delay interval
BIt blocks the program until the function runs
CIt schedules a function to run asynchronously after a delay
DIt immediately runs the function and ignores the delay
Explain how you would use setTimeout and clearTimeout together in a Node.js program.
Think about scheduling a task and then stopping it if needed.
You got /4 concepts.
    Describe what happens if you do not call clearTimeout on a timer you no longer need.
    Consider what happens if a delayed task runs unexpectedly.
    You got /4 concepts.