Multiple "Start" Clicks Speed Up the Timer #71
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Initially, in the Pomodoro Timer project, when we clicked the "Start" button once, the timer would begin as expected.
But if we clicked "Start" again and again, the timer would run faster than normal. Why?
Because each time we clicked the "Start" button, it started a new interval using
setInterval
,which means multiple timers were running at the same time — each one reducing
timeLeft
every second.So the countdown speed increased unnaturally.
Step 1: Preventing Multiple Intervals
To solve this, I added the line:
at the top of the
startTimer()
function.This line checks if the interval is already running. If yes, it just exits and does nothing.
This means now, even if the user clicks "Start" multiple times, it won’t create new intervals.
Result: The timer no longer speeds up when we click "Start" multiple times.
New Issue After Fix: "Start" Didn’t Work After Stop
After adding that check, a new issue appeared. When we clicked "Stop" and then clicked "Start" again, it wouldn’t resume from where it stopped.
Why did this happen?
Because after calling
clearInterval(interval)
in thestopTimer()
function,we forgot to reset the
interval
variable back tonull
.So
interval
still had a value, and our new condition kept blocking the timer from restarting.Step 2: Resetting the Interval Variable
To solve this, I added:
at the end of all three functions:
startTimer()
,stopTimer()
, andresetTimer()
— wherever the interval was cleared.This way, whenever we stop or reset the timer, we also reset the
interval
variable, so it becomes ready to start again when needed.So here I fixed the timer from going faster when we press "Start" multiple times. and also fixed the timer so it can resume again from where i was stopped.