30 Days Of JavaScript

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;

Then at the very bottom of startTimer add:

1timerActive = true;

and finally in resetTimer() add

1timerActive = false;

And that will solve the problem.


Go Pro?

Upgrade to Pro for quizzes, tracked progress and a completion certificate for just $25 🚀

Want more developer tips, tricks and tools?
Then follow me:
FREE Coding Career Quick Start Guide 🚀
Discover the best way to learn to code, how to land a job and valuable resources to help your journey in this free 15 page value packed guide.
Looking to email me? You can get me on my first name at allthecode.co