javascript timer fires up on the first key press

Posted by pedrag on Stack Overflow See other posts from Stack Overflow or by pedrag
Published on 2013-11-06T15:16:58Z Indexed on 2013/11/06 15:53 UTC
Read the original article Hit count: 97

Filed under:
|

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;
    }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html