JavaScript倒计时器不会显示两次

时间:2015-03-19 07:45:17

标签: javascript html

我有以下JavaScript倒计时器,我从Stackoverflow上的帖子中得到了:

function startTimer(duration, display) {
    var start = Date.now(),
        diff,
        minutes,
        seconds;
    function timer() {
        diff = duration - (((Date.now() - start) / 1000) | 0);

        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) {
            start = Date.now() + 1000;
        }
    };

    timer();
    setInterval(timer, 1000);
}

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};

我使用以下HTML来显示它:

<div class="timerConatiner">
    <div class="countDownTimer"><span id="time"></span></div>
</div>

我正在使用的网页具有响应性,时间位置会根据屏幕尺寸发生很大变化。

所以我想到的最好的方法是在HTML中使用2个版本的计时器,然后使用CSS将其中一个设置为display:none,将另一个设置为display:block,具体取决于哪个一个我想要展示的。

问题是只显示HTML中声明的第一个,而另一个将永远不可见。第二个甚至不存在于网页上,因为它没有出现在DOM浏览器中。

我尝试使用单独的ID创建2个计时器,例如:

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time1');
    startTimer(fiveMinutes, display);
};

但这不起作用。

有谁知道发生这种情况的原因?我希望当我创建具有单独ID的2个版本时,它们都会显示,但显然它们不显示?

我确信我可以创建2个版本的计时器,但这似乎并不优雅。

1 个答案:

答案 0 :(得分:1)

工作真的很好我猜oO 添加了带有span的第二个div并将你的onload函数修改为一个高亮度位;)

&#13;
&#13;
function startTimer(duration, display) {
    var start = Date.now(),
        diff,
        minutes,
        seconds;
    function timer() {
        diff = duration - (((Date.now() - start) / 1000) | 0);

        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) {
            start = Date.now() + 1000;
        }
    };

    timer();
    setInterval(timer, 1000);
}

function toggle(){
  if (document.getElementById("time").style.visibility == "hidden"){
    document.getElementById("time").style.visibility = "visible";
    document.getElementById("time2").style.visibility = "hidden";
  }else{
    document.getElementById("time2").style.visibility = "visible";
    document.getElementById("time").style.visibility = "hidden";
  }
}
window.onload = function () {
    var fiveMinutes = 60 * 5;
    var tenMinutes = 60 * 10;    
    display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
    display = document.querySelector('#time2');
    startTimer(tenMinutes, display);
    
};
&#13;
<div class="timerConatiner">
    <div class="countDownTimer"><span id="time"></span></div>
    <div class="countDownTimer2"><span id="time2" style ="visibility: hidden"></span></div>
</div>
<input type = "button" onclick = "toggle()" value = "Toggle"></input>
&#13;
&#13;
&#13;