The bug is the Start button doesn’t guard against being clicked more than once. Every click spins up a new setInterval, and nothing ever clears the old ones, so click it three times and the counter starts ticking three times a second instead of once.
Fix is to keep the interval id and bail out if one is already running:
Spot the Bug answer: setInterval is created on every click but never cleared, causing multiple timers and rapid increments after repeated starts
The fix:
store interval id and clear existing one before starting a new interval: let intervalId; document.getElementById('start').addEventListener('click', function() { if (intervalId) clearInterval(intervalId); intervalId = setInterval(function() { count++; box.textContent = count; }, 1000); });
Why:
Clicking Start multiple times would create multiple intervals, all updating the counter independently, leading to faster and unexpected increments. Clearing the previous interval before starting a new one ensures only a single timer runs.
Nobody got this one. It was a sneaky one.
Close but not quite:
@PipMuse - They identify the issue (multiple intervals) but only partially propose a fix; they suggest guarding but the snippet ends incomplete and misses clearing/storing the interval id properly for reuse.
Bonus round: challenge: Modify the code so that pressing a new “Start” while the timer is running also pauses the timer (a separate Pause button), and add a “Reset” button that stops the timer and resets the counter to zero.