0
0
Node.jsframework~20 mins

Recursive setTimeout vs setInterval in Node.js - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timer Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Output of recursive setTimeout vs setInterval timing
Consider the following Node.js code snippets. What will be the output behavior of each snippet over 3 seconds?

Snippet 1 uses setInterval to print a message every 1 second.
Snippet 2 uses recursive setTimeout to print a message every 1 second.

Which statement best describes the difference in their output timing?
Node.js
Snippet 1:
setInterval(() => {
  console.log('Interval tick');
}, 1000);

Snippet 2:
function recursiveTimeout() {
  console.log('Timeout tick');
  setTimeout(recursiveTimeout, 1000);
}
recursiveTimeout();
ABoth methods cause overlapping executions if the callback takes longer than 1 second.
BBoth print exactly every 1 second with no delay accumulation.
CsetInterval waits for the callback to finish before next tick; recursive setTimeout runs ticks in parallel causing overlap.
DsetInterval prints every 1 second regardless of execution time; recursive setTimeout waits for the callback to finish before scheduling next, preventing overlap.
Attempts:
2 left
💡 Hint
Think about how each method schedules the next execution relative to the callback duration.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in recursive setTimeout usage
Which option contains a syntax error that prevents the recursive setTimeout from working correctly?
Node.js
function tick() {
  console.log('Tick');
  setTimeout(tick, 1000)
}
A
function tick() {
  console.log('Tick');
  setTimeout(tick 1000);
}
B
function tick() {
  console.log('Tick');
  setTimeout(tick, 1000);
}
C
function tick() {
  console.log('Tick');
  setTimeout(tick, '1000');
}
D
function tick() {
  console.log('Tick');
  setTimeout(tick, 1000)
}
Attempts:
2 left
💡 Hint
Check the syntax of the setTimeout function call arguments.
state_output
advanced
2:00remaining
State after 3 recursive setTimeout calls with varying delays
Given this code, what will be the value of count after approximately 3500 milliseconds?
Node.js
let count = 0;
function tick() {
  count++;
  console.log('Tick', count);
  setTimeout(tick, count * 1000);
}
tick();
Acount will be 1
Bcount will be 2
Ccount will be 3
Dcount will be 4
Attempts:
2 left
💡 Hint
Each delay increases by 1000ms multiplied by the current count.
🔧 Debug
advanced
2:00remaining
Why does this recursive setTimeout cause a memory leak?
Examine this code snippet. Why might it cause increasing memory usage over time?
Node.js
function tick() {
  setTimeout(() => {
    console.log('Tick');
    tick();
  }, 1000);
}
tick();
ABecause tick is called inside setTimeout, causing infinite recursion without delay.
BBecause each setTimeout callback creates a new closure that references the previous one, causing memory to accumulate.
CBecause setTimeout is called inside an arrow function, which leaks memory in Node.js.
DBecause the console.log inside setTimeout causes memory to grow indefinitely.
Attempts:
2 left
💡 Hint
Think about how closures capture variables and references in recursive asynchronous calls.
🧠 Conceptual
expert
2:30remaining
Choosing between setInterval and recursive setTimeout for precise timing
You need to run a task every 1000ms, but the task duration varies and can sometimes take longer than 1000ms. Which approach ensures the task never overlaps and runs immediately after the previous finishes?
AUse recursive setTimeout scheduling the next call only after the current task finishes.
BUse setInterval with 1000ms delay; it queues tasks even if previous is still running.
CUse setInterval but clear and reset it inside the callback to prevent overlap.
DUse recursive setTimeout with a fixed 1000ms delay regardless of task duration.
Attempts:
2 left
💡 Hint
Consider how each method handles task duration longer than the interval.