JavaScript count down, add hours & minutes

前端 未结 3 1999
无人共我
无人共我 2021-02-11 04:03

So, I have the below (seconds countdown) in good order. But! I am trying to add hours & minutes as apart of the count down as well. Ideally keeping the same structure, and j

3条回答
  •  别那么骄傲
    2021-02-11 04:50

    Something like this:

    var count = 30;
    var counter = setInterval(timer, 1000); //1000 will  run it every 1 second
    
    function timer() {
        count = count - 1;
        if (count == -1) {
            clearInterval(counter);
            return;
        }
    
        var seconds = count % 60;
        var minutes = Math.floor(count / 60);
        var hours = Math.floor(minutes / 60);
        minutes %= 60;
        hours %= 60;
    
        document.getElementById("timer").innerHTML = hours + "hours " + minutes + "minutes and" + seconds + " seconds left on this Sale!"; // watch for spelling
    }
    

提交回复
热议问题