Bird
0
0

What is wrong with this recursive setTimeout implementation?

medium📝 Debug Q6 of 15
Node.js - Timers and Scheduling
What is wrong with this recursive setTimeout implementation?
function repeat() {
  setTimeout(repeat(), 1000);
  console.log('Hi');
}
repeat();
AThe delay of 1000ms is too short for recursion
BThe function is invoked immediately instead of passing a reference to setTimeout
CThe console.log should be inside the setTimeout callback
DThe function repeat is missing a return statement
Step-by-Step Solution
Solution:
  1. Step 1: Analyze setTimeout argument

    Using repeat() calls the function immediately instead of passing it as a callback.
  2. Step 2: Correct usage

    Pass the function reference without parentheses: setTimeout(repeat, 1000);
  3. Final Answer:

    The function is invoked immediately instead of passing a reference to setTimeout -> Option B
  4. Quick Check:

    Pass function reference, not invocation [OK]
Quick Trick: Use function name without parentheses in setTimeout [OK]
Common Mistakes:
  • Calling the function inside setTimeout instead of passing reference
  • Placing console.log outside the callback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes