Fix bug: Clock can't be stopped when 'Start' clicked while running

This commit is contained in:
Petr Glivicky 2020-12-04 20:40:01 +01:00
parent e1a3f634a4
commit d4c23dcf37
2 changed files with 13 additions and 5 deletions

View file

@ -43,12 +43,17 @@
}
function clockStart() {
timerId = setInterval(update, 1000);
// set a new interval only if the clock is stopped
// otherwise we would rewrite the timerID reference to the running interval and wouldn't be able to stop the clock ever again
if (!timerId) {
timerId = setInterval(update, 1000);
}
update(); // <-- start right now, don't wait 1 second till the first setInterval works
}
function clockStop() {
clearInterval(timerId);
timerId = null; // <-- clear timerID to indicate that the clock has been stopped, so that it is possible to start it again in clockStart()
}
clockStart();