javascript timer fires up on the first key press
- by pedrag
I have a html page with a timer in it. I'm starting the timer with the keypress event, but i want it to execute only for the first key. I'm using a variable to catch the total keys in an other function which there were pressed and i have something like that: if(totaAttempts==1) start the timer, but with this solution the timer starts correctly, but is stomps when a key is pressed again. Any better ideas?
Thanks in advance
function setTime() {
if (totalAttempts == 1) {
++totalSeconds;
secondsLabel.innerHTML = pad(totalSeconds % 60);
minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
}
}
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}