当coundown为零时如何更改文本

时间:2015-10-06 15:11:12

标签: javascript php

你好我的倒计时没有停在零我需要在倒计时到零时更改我的测试,这个倒计时在零值后再次开始我需要在倒计时后替换值为零..... $ countdown = 50

production.rb

PHP In body

function startTimer(duration, display) {
var start = Date.now(),
    diff,
    minutes,
    seconds;
function timer() {
    // get the number of seconds that have elapsed since 
    // startTimer() was called
    diff = duration - (((Date.now() - start) / 1000) | 0);

    // does the same job as parseInt truncates the float
    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds; 

    if (diff <= 0) {
        // add one second so that the count down starts at the full duration
        // example 05:00 not 04:59
        start = Date.now() + 1000;
    }
};
// we don't want to wait a full second before the timer starts
timer();
setInterval(timer, 1000);
}

window.onload = function () {
var fiveMinutes = <?php echo $countdown?>

    display = document.querySelector('#time');
startTimer(fiveMinutes, display);
  };

1 个答案:

答案 0 :(得分:0)

您应该将setInterval()调用保存到变量。

var myTimer = setInterval();

这样您可以在以后引用它。然后,您可以在函数内调用以检查某个条件(在倒计时达到0的情况下),然后使用clearInterval()结束计时器。

Topic covering clearInterval()