Assesment wanted for Active learning: Creating your own stopwatch!

Hey guys,

I tried my hand at the active learning example on this page: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals

Instead of doing it the easy way, like the finished example, I created my Stopwatch using a Date () object for extra accuracy.

However, calculating the stopwatch display value by subtracting StartTime fromDate.now() introduced the need to ‘remember’ the amount of time already on the clock in case the stopwatch was re-started.

I’ve worked around this by ‘reading’ the clock string from the page when the stop button is pressed, but I’m sure there has to be a better way…

Here’s my code: https://jsfiddle.net/Rynout/Lax06n4z/1/

Any pointers?

Thanks a ton in advance!

Hello @rynout

you doing great

you need to clear the interval so when you resume it continu where it left not just keep counting in the background

clearInterval(stopwatch);

not sure if there a better way for your question

and have a nice day

Hey @justsomeone,

Thanks so much for replying.

I’ve now made the stopwatch more accurate by replacing the former runTimeSec variable with a runTimeCentiSec variable, and adding an extra digit to the clock representing a tenth of a second (a centisecond). Updated jsfiddle.

Yes, it’s some extra lines of code but my stopwatch is more accurate than the finished example in the course so I’m pretty happy with it.

Any feedback on the possible downsides of my solution is much appreciated! :slight_smile:

Also, I wonder if I took this one step further and let my interval run every 1 ms, if I would run into some inaccuracies or other problems? :thinking:

you very welcome @rynout

and this is great

i am not sure about that specially the accuracy but you can try if you like and see how it work and let me know if you go for it

well done and have a nice day