Bird
0
0

You want to create a timer that logs "Tick" every second but adjusts the delay dynamically based on system load. Which approach is best?

hard📝 Conceptual Q8 of 15
Node.js - Timers and Scheduling
You want to create a timer that logs "Tick" every second but adjusts the delay dynamically based on system load. Which approach is best?
AUse recursive <code>setTimeout</code> and calculate delay before each call.
BUse <code>setInterval</code> with a fixed 1000ms delay.
CUse a loop with <code>setTimeout</code> called once outside the loop.
DUse <code>setInterval</code> and clear it every time before setting again.
Step-by-Step Solution
Solution:
  1. Step 1: Understand dynamic delay requirement

    Dynamic delay means the wait time can change based on conditions like system load.
  2. Step 2: Match approach to requirement

    Recursive setTimeout allows recalculating delay before scheduling next call, unlike fixed setInterval.
  3. Final Answer:

    Use recursive setTimeout and calculate delay before each call. -> Option A
  4. Quick Check:

    Dynamic delay needs recursive setTimeout = B [OK]
Quick Trick: Calculate delay inside recursive setTimeout for dynamic timing [OK]
Common Mistakes:
  • Using fixed setInterval for dynamic delays
  • Calling setTimeout once outside loop
  • Clearing and resetting setInterval repeatedly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes