48小时倒计时,24小时后复位

时间:2018-08-08 10:41:15

标签: javascript jquery

我正在倒计时。根据实际时间,应该倒计时48小时直到第二天凌晨12点。但是倒数计时应在实际时间的凌晨12点重新设置,然后从48h开始再次倒数直到第二天的凌晨12点。

此刻,根据直到24:00的实际时间,它只倒计时24小时。

这怎么办?

这是我所拥有的:

var date;
var display = document.getElementById('time');
setInterval(function(){
    date = new Date();
    var currenthours = date.getHours();
    var hours;
    var minutes;
    var secondes;
    if (currenthours != 23){
        if (currenthours < 23)
            hours = 23 - currenthours;
        else hours = 23 + (23 - currenthours);
        minutes = 60 - date.getMinutes();
        secondes = 60 - date.getSeconds();
    display.innerHTML = hours + 'h ' + minutes + 'm ' +secondes + 's';
    }
    else display.innerHTML = 'LIVE NOW';
},1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='time'></span>

1 个答案:

答案 0 :(得分:1)

您可以将24小时添加到结果中,以获取48小时间隔。

var date;
    var display = document.getElementById('time');
    setInterval(function(){
        date = new Date();
        var currenthours = date.getHours();
        var hours;
        var minutes;
        var secondes;
        if (currenthours != 23){
            if (currenthours < 23)
                hours = 47 - currenthours;
            else hours = 47 + (23 - currenthours);
            minutes = 60 - date.getMinutes();
            secondes = 60 - date.getSeconds();
        display.innerHTML = hours + 'h ' + minutes + 'm ' +secondes + 's';
        }
        else display.innerHTML = 'LIVE NOW';
    },1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="time"></div>