When I click on the button it just speeds up the timer. The more I click the faster it gets
playBtn.addEventListener('click', () => {
if (timerStatus == "Stopped") {
timerInterval = window.setInterval(stopWatch, 1000);
document.getElementById('startstopBtn').innerHTML = ` <i class="fa-solid fa-play" id="play"></i>`
timerStatus = "Stopped"
}
I tried to make it happen only once.
3
Answers
use the anyone method to solve
or
or
It seems like you want to speed up the timer every time the button is clicked. To achieve this, you need to decrease the interval between each tick of the timer, effectively making it tick faster. The current implementation you’ve shown doesn’t accurately reflect this behavior.
The timer is speeding up because you are calling new intervals again and again when you are clicking the button and each of them is calling with its own interval. It is like starting timers on multiple clocks one after another.
Like @Barmar mentioned, the solution is to clear the interval on each new click if it already exists. I see that you are creating a new reset button to do this, which is not necessary.
For it to work as expected, you need to do something like this: