Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to schedule a function to run after 2 seconds.
Node.js
setTimeout(() => { console.log('Hello!'); }, [1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 instead of 2000 for 2 seconds delay.
Using 0.002 which is 2 microseconds, too short.
✗ Incorrect
The setTimeout delay is in milliseconds, so 2000 means 2 seconds.
2fill in blank
mediumComplete the code to cancel a scheduled timeout using its ID.
Node.js
const timerId = setTimeout(() => { console.log('Run later'); }, 3000);
clearTimeout([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the delay time (3000) instead of the timer ID.
Passing
null or the function name.✗ Incorrect
You must pass the timer ID returned by setTimeout to clearTimeout to cancel it.
3fill in blank
hardFix the error in the code to properly cancel the timeout.
Node.js
let id = setTimeout(() => console.log('Oops'), 5000); clearTimeout([1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name like
timeoutId.Passing the delay time (5000) instead of the ID.
✗ Incorrect
The variable id holds the timer ID and must be passed to clearTimeout.
4fill in blank
hardFill both blanks to schedule a message and then cancel it before it runs.
Node.js
const [1] = setTimeout(() => console.log('Bye!'), 4000); [2]([1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
setTimeout instead of clearTimeout to cancel.Not storing the timer ID in a variable.
✗ Incorrect
Store the timer ID in timer and pass it to clearTimeout to cancel.
5fill in blank
hardFill all three blanks to create a timeout, cancel it, and then schedule a new one.
Node.js
const [1] = setTimeout(() => console.log('First'), 3000); [2]([1]); const [3] = setTimeout(() => console.log('Second'), 3000);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not cancelling the first timer before creating the second.
Using the same variable name for both timers.
✗ Incorrect
Use firstTimer to store the first timeout, cancel it with clearTimeout, then create secondTimer for the new timeout.