Bird
0
0

Consider this code snippet:

medium📝 component behavior Q5 of 15
Node.js - Timers and Scheduling
Consider this code snippet:
let timerId = setInterval(() => console.log('Tick'), 500);
setTimeout(() => clearInterval(timerId), 1600);
What will be the output?
ANo output because clearInterval is called immediately
B'Tick' printed 3 times at 0.5s, 1s, and 1.5s
C'Tick' printed once after 1.6 seconds
D'Tick' printed 4 times at 0.5s, 1s, 1.5s, and 2s
Step-by-Step Solution
Solution:
  1. Step 1: Understand setInterval timing

    setInterval runs every 500ms, so ticks at 0.5s, 1s, 1.5s, 2s, etc.
  2. Step 2: Analyze clearInterval timing

    clearInterval is called after 1600ms (1.6s), so it stops before the 2s tick.
  3. Final Answer:

    'Tick' printed 3 times at 0.5s, 1s, and 1.5s -> Option B
  4. Quick Check:

    clearInterval stops after 1.6s, so 3 ticks printed [OK]
Quick Trick: clearInterval stops next interval after call time [OK]
Common Mistakes:
  • Counting tick at 2 seconds after clearInterval
  • Thinking clearInterval stops immediately before any tick
  • Confusing setTimeout and setInterval timings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes