Bird
Raised Fist0
Node.jsframework~20 mins

Recursive setTimeout vs setInterval in Node.js - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main difference between setInterval and recursive setTimeout in Node.js?
easy
A. setInterval can only run synchronous code, recursive setTimeout can run asynchronous code.
B. setInterval runs tasks at fixed intervals regardless of task duration, recursive setTimeout waits for the task to finish before scheduling the next.
C. setInterval runs only once, recursive setTimeout runs repeatedly.
D. setInterval automatically adjusts intervals based on task duration, recursive setTimeout does not.

Solution

  1. Step 1: Understand setInterval behavior

    setInterval schedules a function to run repeatedly at fixed time intervals without waiting for the previous run to finish.
  2. Step 2: Understand recursive setTimeout behavior

    Recursive setTimeout schedules the next run only after the current function completes, avoiding overlap.
  3. Final Answer:

    setInterval runs tasks at fixed intervals regardless of task duration, recursive setTimeout waits for the task to finish before scheduling the next. -> Option B
  4. Quick Check:

    Task overlap control [OK]
Hint: Remember: recursive waits, interval runs fixed times [OK]
Common Mistakes:
  • Thinking setInterval waits for task completion
  • Confusing recursive setTimeout with single timeout
  • Assuming setInterval adjusts timing automatically
2. Which of the following is the correct syntax to implement a recursive setTimeout that logs "Hello" every 2 seconds?
easy
A. setTimeout(() => { console.log('Hello'); }, 2000); setTimeout(() => { console.log('Hello'); }, 2000);
B. setInterval(() => { console.log('Hello'); }, 2000);
C. function repeat() { setTimeout(() => { console.log('Hello'); repeat(); }, 2000); } repeat();
D. function repeat() { setInterval(() => { console.log('Hello'); repeat(); }, 2000); } repeat();

Solution

  1. Step 1: Identify recursive setTimeout pattern

    The function calls setTimeout inside itself after logging, ensuring repeated delayed calls.
  2. Step 2: Check syntax correctness

    function repeat() { setTimeout(() => { console.log('Hello'); repeat(); }, 2000); } repeat(); defines a function that calls itself inside setTimeout, then starts it by calling repeat().
  3. Final Answer:

    function repeat() { setTimeout(() => { console.log('Hello'); repeat(); }, 2000); } repeat(); -> Option C
  4. Quick Check:

    Recursive call inside setTimeout [OK]
Hint: Recursive setTimeout calls itself inside timeout [OK]
Common Mistakes:
  • Using setInterval instead of recursive setTimeout
  • Not calling the recursive function initially
  • Calling setTimeout multiple times without recursion
3. Consider this code snippet:
let count = 0;
function tick() {
  console.log(count);
  count++;
  if (count < 3) {
    setTimeout(tick, 1000);
  }
}
tick();
What will be the output and timing behavior?
medium
A. Logs 0 once, then stops without further logs.
B. Logs 0, 1, 2 immediately without delay, then stops.
C. Logs 0, 1, 2 every 1 second simultaneously, overlapping.
D. Logs 0, 1, 2 each after 1 second delay sequentially, then stops.

Solution

  1. Step 1: Analyze recursive setTimeout calls

    Function tick logs count, increments it, and schedules next call after 1 second if count < 3.
  2. Step 2: Trace output and timing

    Logs 0 immediately, then after 1s logs 1, after another 1s logs 2, then stops because count reaches 3.
  3. Final Answer:

    Logs 0, 1, 2 each after 1 second delay sequentially, then stops. -> Option D
  4. Quick Check:

    Recursive timeout delays [OK]
Hint: Recursive timeout delays each call by 1 second [OK]
Common Mistakes:
  • Assuming logs happen immediately without delay
  • Thinking logs overlap simultaneously
  • Confusing setTimeout with setInterval behavior
4. Identify the problem in this code using recursive setTimeout:
function repeat() {
  setTimeout(() => {
    console.log('Tick');
  }, 1000);
  repeat();
}
repeat();
medium
A. The function calls itself immediately causing a stack overflow.
B. The timeout delay is too short to see output.
C. The console.log is outside the timeout callback.
D. The function never calls itself, so it runs only once.

Solution

  1. Step 1: Examine recursion timing

    The function repeat calls itself immediately after scheduling setTimeout, without waiting for the timeout to finish.
  2. Step 2: Identify consequence

    This causes infinite immediate recursion, leading to stack overflow before any timeout callback runs.
  3. Final Answer:

    The function calls itself immediately causing a stack overflow. -> Option A
  4. Quick Check:

    Immediate recursion without delay [OK]
Hint: Call recursive function inside timeout callback only [OK]
Common Mistakes:
  • Placing recursive call outside timeout callback
  • Assuming timeout delays recursion automatically
  • Ignoring stack overflow risk
5. You want to run a task every 3 seconds but ensure the task never overlaps if it takes longer than 3 seconds. Which approach is best?
hard
A. Use recursive setTimeout scheduling the next call only after task finishes.
B. Use setInterval with 3000ms delay and ignore task duration.
C. Use setTimeout once without recursion.
D. Use setInterval with 1000ms delay and check task status inside.

Solution

  1. Step 1: Understand overlap risk with setInterval

    setInterval runs tasks at fixed intervals regardless of task duration, causing overlap if task takes longer than interval.
  2. Step 2: Use recursive setTimeout to control timing

    Recursive setTimeout schedules the next run only after the current task finishes, preventing overlap.
  3. Final Answer:

    Use recursive setTimeout scheduling the next call only after task finishes. -> Option A
  4. Quick Check:

    Prevent overlap with recursive timeout [OK]
Hint: Recursive timeout waits for task end before next call [OK]
Common Mistakes:
  • Using setInterval ignoring task duration
  • Not scheduling next call after task completion
  • Using too short intervals causing overlap