Skip to content

Multiple "Start" Clicks Speed Up the Timer #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

disha2553
Copy link

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:

if (interval) return;

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 the stopTimer() function,
we forgot to reset the interval variable back to null.
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:

interval = null;

at the end of all three functions:
startTimer(), stopTimer(), and resetTimer() — 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant