So, I've written a timer to start when the page loads, and to stop when the user clicks a "finished" button:
<br><button id = "finish" onclick = "stopTimer();">Done</button>
My code works fine, but I was just wondering if there's a simpler/easier/faster way to implement a timer in Javascript?JavaScript.
//Stop timer function
function stopTimer() {
clearInterval(time);
}
//Start timer function
var secTime = 0,
minTime = 0,
hourTime = 0;
var time = setInterval(function(){
var maxSec = 59,
maxMin = 59,
maxHour = 59;
if(secTime > maxSec){
minTime++;
if(minTime > maxMin){
hourTime++;
if(hourTime > maxHour){
hourTime = 0;
minTime = 0;
secTime = 0;
}
minTime = 0
}
secTime = 0;
}
var newSec = (secTime.toString().length == 1) ? '0' + secTime : secTime,
newMin = (minTime.toString().length == 1) ? '0' + minTime : minTime,
newHour = (hourTime.toString().length == 1) ? '0' + hourTime : hourTime;
document.getElementById('timer').innerHTML = newHour + ':' + newMin + ':' + newSec;
secTime++;
}, 1000);