0
0
Node.jsframework~20 mins

setTimeout and clearTimeout in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Node.js code output?
Consider the following code snippet using setTimeout and clearTimeout. What will be printed to the console?
Node.js
const timer = setTimeout(() => {
  console.log('Hello after 1 second');
}, 1000);

clearTimeout(timer);

console.log('Timer cleared');
ATimer cleared
BNo output
CHello after 1 second
DHello after 1 second\nTimer cleared
Attempts:
2 left
💡 Hint
Think about what clearTimeout does to a scheduled timer.
state_output
intermediate
2:00remaining
What is the value of count after this code runs?
Look at this Node.js code using setTimeout. What will be the final value of count after 1500 milliseconds?
Node.js
let count = 0;

const timer = setTimeout(() => {
  count += 1;
}, 1000);

setTimeout(() => {
  clearTimeout(timer);
}, 500);

setTimeout(() => {
  console.log(count);
}, 1500);
A1
Bundefined
C0
DThrows an error
Attempts:
2 left
💡 Hint
When is the timer cleared compared to when it would run?
📝 Syntax
advanced
2:00remaining
Which option correctly cancels a repeating timer?
You want to stop a repeating timer created with setInterval. Which code snippet correctly cancels it?
Node.js
const intervalId = setInterval(() => {
  console.log('Tick');
}, 1000);

// Which line correctly stops the interval?
AstopInterval(intervalId);
BclearInterval(intervalId);
CcancelTimeout(intervalId);
DclearTimeout(intervalId);
Attempts:
2 left
💡 Hint
Remember the difference between setTimeout and setInterval cancellation functions.
🔧 Debug
advanced
2:00remaining
Why does this timer never stop?
This code is intended to print 'Hello' every second and stop after 3 seconds. Why does it keep printing forever?
Node.js
const timer = setTimeout(function repeat() {
  console.log('Hello');
  setTimeout(repeat, 1000);
}, 1000);

setTimeout(() => {
  clearTimeout(timer);
  console.log('Stopped');
}, 3000);
ABecause clearTimeout only cancels the first timer, but new timers keep being created inside repeat.
BBecause clearTimeout is called too late after 3 seconds.
CBecause setTimeout cannot be used recursively.
DBecause the timer variable is not declared with let or const.
Attempts:
2 left
💡 Hint
Look at how many timers are created and which one is cleared.
🧠 Conceptual
expert
3:00remaining
What is the behavior of this code snippet?
Analyze this Node.js code. What will be the output and why?
Node.js
let count = 0;

const timer = setTimeout(() => {
  console.log('Timeout 1:', count);
}, 1000);

const timer2 = setTimeout(() => {
  console.log('Timeout 2:', count);
}, 1500);

count = 5;

clearTimeout(timer);

setTimeout(() => {
  console.log('Final count:', count);
}, 2000);
ATimeout 1: 5\nFinal count: 5
BTimeout 1: 0\nTimeout 2: 5\nFinal count: 5
CTimeout 1: 5\nTimeout 2: 5\nFinal count: 5
DTimeout 2: 5\nFinal count: 5
Attempts:
2 left
💡 Hint
Consider which timers are cleared and when the variable count changes.