Bird
0
0

Why might a recursive setTimeout be preferred over setInterval when the callback function takes variable time to execute?

medium📝 Conceptual Q7 of 15
Node.js - Timers and Scheduling
Why might a recursive setTimeout be preferred over setInterval when the callback function takes variable time to execute?
ABecause <code>setInterval</code> cannot be stopped once started.
BBecause <code>setInterval</code> waits for the function to finish before next call.
CBecause recursive <code>setTimeout</code> waits for the function to finish before scheduling next call.
DBecause recursive <code>setTimeout</code> runs the function multiple times simultaneously.
Step-by-Step Solution
Solution:
  1. Step 1: Understand execution timing of setInterval

    setInterval schedules calls at fixed intervals regardless of function duration, possibly overlapping calls.
  2. Step 2: Understand recursive setTimeout timing

    Recursive setTimeout schedules next call only after current function finishes, avoiding overlap.
  3. Final Answer:

    Because recursive setTimeout waits for the function to finish before scheduling next call. -> Option C
  4. Quick Check:

    Recursive setTimeout waits for completion = D [OK]
Quick Trick: Recursive setTimeout avoids overlapping calls by waiting [OK]
Common Mistakes:
  • Thinking setInterval waits for function to finish
  • Believing recursive setTimeout runs multiple calls simultaneously
  • Assuming setInterval cannot be stopped

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes