我的Javascript倒数计时器无法正常运作

时间:2018-09-19 23:09:04

标签: javascript date timer countdown

我是编码新手,所以我的代码有点混乱。因此,我正在制作一个5分钟的倒数计时器,它确实进行倒数计时,但它似乎跳过了一些数字或出现了奇怪的延迟。为什么是这样? 这是链接https://jsfiddle.net/m82ube1y/9/

var timer= document.getElementById("timer").children;
var edDate = new Date();
var endDate = edDate.setMinutes(edDate.getMinutes()+5) ;

setInterval(time, 1000);

function time() {

var currentDate = new Date().getTime();

var countdowntime = endDate-currentDate;

var days = Math.floor((((countdowntime/1000)/60)/60)/24);
var hours = Math.floor((((countdowntime/1000)/60)/60))%24;
var minutes = Math.floor(((countdowntime/1000)/60))%60;
var seconds = Math.floor((countdowntime/1000))%60;
    timer[0].textContent = days;
    timer[1].textContent = hours;
    timer[2].textContent = minutes;
    timer[3].textContent = seconds;

    timer[3].classList.add("rotate");
    setTimeout(function(){f(3);},500);
    function f(x){timer[x].classList.remove("rotate")}

    if(timer[3].textContent==59){ 
        timer[2].classList.add("rotate");
        setTimeout(function(){f(2);},500);}
}

编辑:我发现它在Firefox中可以正常工作,但在chrome中不能正常工作。仍然不明白为什么。

0 个答案:

没有答案