如何修复JavaScript计时器,以使秒数最多达到60秒,然后重置为零

时间:2019-02-06 18:48:59

标签: javascript timer

我正在设置一个计时器,但是目前秒数是成千上万,而不是60秒后重置为零。有快速解决方案吗? 这是我正在使用的代码:

var running = 0;
function startPause(){
if(running == 0){
    running = 1;
    increment();
    document.getElementById("start").innerHTML = "Pause";
}else{
    running = 0;
    document.getElementById("start").innerHTML = "Resume";
}
};

function reset(){
running = 0;
time = 0;
document.getElementById("output").innerHTML = "00:00";
document.getElementById("start").innerHTML = "Start";
};
function increment(){
if(running == 1){
    setTimeout(function(){
        time++;
        var mins = Math.floor(time / 10 / 60);
        if(mins <= 9){
            mins = "0" + mins;
        }
        var secs = Math.floor(time / 10);
        if(secs <= 9){
            secs = "0" + secs;
        }
        document.getElementById("output").innerHTML = mins + ":" + secs;
        increment();
    }, 100);
}};

1 个答案:

答案 0 :(得分:0)

您可以使用模数%60

将剩下的部分除以60

例如127%60 = 7