重置后JavaScript计时器无法启动

时间:2015-09-11 11:29:22

标签: javascript timer reset countdowntimer

计时器,开始/暂停按钮和重置按钮均按预期工作。

它似乎唯一的错误是,当我运行计时器并重置它时,它 不会重新开始。只有在按下“开始/暂停”按钮2次后,它才会再次运行。

现场演示:http://codepen.io/Michel85/full/pjjpYY/

代码:

var timerTime; 
var time = 1500;
var currentTime;
var flag = 0;
var calculateTime

// Start your Timer
function startTimer(time) {
        document.getElementById("btnUp").disabled = true;
        document.getElementById("btnDwn").disabled = true; 
        timerTime = setInterval(function(){ 
        showTimer(); 
        return flag = 1;
    }, 1000);
}

// Reset function
function resetTimer(){
    clearInterval(timerTime);
    document.getElementById('showTime').innerHTML=(calculateTime(1500));
    document.getElementById("btnUp").disabled = false;
    document.getElementById("btnDwn").disabled = false;
    return time=1500;
}

// Pause function
function pauseTimer(){
    clearInterval(timerTime);
    currentTime = time;
    document.getElementById('showTime').innerHTML=(calculateTime(time));
    return flag = 0;
}

// Update field with timer information
function showTimer(){
    document.getElementById("showTime").innerHTML=(calculateTime(time));
    flag = 1;
    if(time < 1){
        resetTimer();
    }
    time--;
};

// Toggle function (Pause/Run)
function toggleTimmer(){
    if(flag == 1){
        pauseTimer();
    } else {
        startTimer();
    }
}
/* 
    Round-time up or down 
*/

 // Set timer Up
function timeUp(){
    time += 60;
    document.getElementById("showTime").innerHTML=(calculateTime(time));
    return time;
}

// Set timer Down
function timeDown(){
    if(time > 60){
        time-=60;
    }
    document.getElementById("showTime").innerHTML=(calculateTime(time));
    return time;
}

/*
    Break-time up down
*/
 // Set timer Up
function breakUp(){
    time += 60;
    document.getElementById("showTime").innerHTML=(calculateTime(time));
    return time;
}

// Set timer Down
function breakDown(){
    if(time > 60){
        time-=60;
    }
    document.getElementById("showTime").innerHTML=(calculateTime(time));

    return time;
}

// Calculate the Days, Hours, Minutes, seconds and present them in a digital way. 
function calculateTime(totalTime) {

    // calculate days
    var days = Math.floor(totalTime / 86400);
    totalTime = totalTime % 86400

    // calculate hours
    var hours = Math.floor(totalTime / 3600);
    totalTime = totalTime % 3600;

    // calculate minutes
    var minutes = Math.floor(totalTime / 60);
    totalTime = totalTime % 60;

    // calculate seconds
    var seconds = Math.floor(totalTime);

    function convertTime(t) {
        return ( t < 10 ? "0" : "" ) + t;
    }

    // assign the variables
    days = convertTime(days);
    hours = convertTime(hours);
    minutes = convertTime(minutes);
    seconds = convertTime(seconds);

    // Make sure the "00:" is present if empty.
    if(days !== "00"){
        var currentTimeString = days + ":" + hours + ":" + minutes + ":" + seconds;
        return currentTimeString;
    } else if (hours !== "00"){
        var currentTimeString = hours + ":" + minutes + ":" + seconds;
        return currentTimeString
    } else if(minutes !== "0:00"){
        var currentTimeString = minutes + ":" + seconds;
        return currentTimeString
    } else {
        var currentTimeString = seconds;
        return currentTimeString
    }
}

欢迎任何帮助。 提前谢谢。

电贺, 米歇尔

2 个答案:

答案 0 :(得分:1)

resetTimer需要将flag设置为0。如果将其设置为1,则toggleTimer将调用pauseTimer而不是startTimer

BTW,对标志使用布尔值(true/false)是更好的风格。

答案 1 :(得分:0)

只需删除clearInterval(timerTime);在函数resetTimer();

function resetTimer() {
  //clearInterval(timerTime);
  document.getElementById('showTime').innerHTML = (calculateTime(300));
  document.getElementById("btnUp").disabled = false;
  document.getElementById("btnDwn").disabled = false;
  return time = 300;
}