Day 23: Timer
Lesson 5: Timer: 5
When you click Stop and then Start it resets the timer. This isn't good timer behavior in my books, it should carry on from where we stopped it.
Let's fix that.
Add this line towards the top of your JavaScript below let timerInterval
.
1let timerActive = false;
When we start the timer we will set this to true
and we will set it to false
when we click Reset. This means when the timer is stopped and then restarted we can check and see if timerActive
is true
and if it is we can just let the timer carry on and not get the form input again.
Add this to the top of startTimer()
1if (timerActive) { 2 timerInterval = setInterval(updateTimer, 1000); 3 setButtons("running"); 4 return; 5}
Then at the very bottom of startTimer
add:
1timerActive = true;
and finally in resetTimer()
add
1timerActive = false;
And that will solve the problem.