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…
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!
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?