2017-04-10 21 views
0

24小时倒计时脚本,我需要在我的网站24小时倒计时,在每天11:00重置,之后再次启动24小时周期。命中每天00:00 11:00

我不需要这个脚本来控制什么,我只需要它在那里,供游人看到的,所以当他们在10:00访问例如会看到还剩1小时上的时钟,并住了下来计数格式:小时,分钟,秒 ,我需要它忽略客户的时区。

我发现了类似的答案,但有1个小时的窗口,它之后重置,我需要它立即复位,我怎么可以对其进行编辑,以满足我的要求?

这是我发现了什么(这算至21:00将在一楼小时的窗口,比重新开始):

var date; 
var display = document.getElementById('time'); 

$(document).ready(function() { 
    getTime('GMT', function(time){ 
     date = new Date(time); 
    });  
}); 

setInterval(function() { 
    date = new Date(date.getTime() + 1000); 

    var currenthours = date.getHours(); 
    var hours; 
    var minutes; 
    var seconds; 
    if (currenthours != 21){ 
     if (currenthours < 21) { 
      hours = 20 - currenthours; 
     } else { 
      hours = 21 + (24 - currenthours); 
     } 
     minutes = 60 - date.getMinutes(); 
     seconds = 60 - date.getSeconds(); 

     if (minutes < 10) { 
      minutes = '0' + minutes; 
     } 
     if (seconds < 10) { 
      seconds = '0' + seconds; 
     } 

     display.innerHTML = hours + ':' + minutes + ':' +seconds; 
    } else { 
     display.innerHTML = 'LIVE NOW'; 
    } 
}, 1000); 

function getTime(zone, success) { 
    var url = 'http://json-time.appspot.com/time.json?tz=' + zone, 
     ud = 'json' + (+new Date()); 
    window[ud]= function(o){ 
     success && success(new Date(o.datetime)); 
    }; 
    document.getElementsByTagName('head')[0].appendChild((function(){ 
     var s = document.createElement('script'); 
     s.type = 'text/javascript'; 
     s.src = url + '&callback=' + ud; 
     return s; 
    })()); 
} 
+1

'currenthours!= 21'只检查小时,无论其21:00或21:59,或其间的任何事物。 – Qirel

回答

0

我猜一个1数个复位是由于单独验证小时。请尝试以下代码:

var date; 
var display = document.getElementById('time'); 

$(document).ready(function() { 
    getTime('GMT', function(time){ 
     date = new Date(time); 
    });  
}); 

setInterval(function() { 
    date = new Date(date.getTime() + 1000); 

    var currenthours = date.getHours(); 
    var currentSecs = date.getSeconds(); 
    var hours; 
    var minutes; 
    var seconds; 
    if (currenthours == 23 && currentsecs == 0){ 
     display.innerHTML = 'LIVE NOW'; 
    } else { 
     if (currenthours < 23) { 
      hours = 22 - currenthours; 
     } else { 
      hours = 23; 
     } 
     minutes = 60 - date.getMinutes(); 
     seconds = 60 - date.getSeconds(); 

     if (minutes < 10) { 
      minutes = '0' + minutes; 
     } 
     if (seconds < 10) { 
      seconds = '0' + seconds; 
     } 
    display.innerHTML = hours + ':' + minutes + ':' +seconds; 
    } 
}, 1000); 
0

即使在夏令时更改期间也要获得准确的日持续时间,您应该坚持使用日期算术。

function time() { 
    var d1 = new Date(); 
    var d2 = Date.UTC(d1.getUTCFullYear(), 
         d1.getUTCMonth(), 
         d1.getUTCDate() + (d1.getUTCHours() < 11 ? 0 : 1), 
         11); 
    var dh = d2 - d1; 
    var hours = Math.floor(dh/3600000); 
    var dm = dh - 3600000 * hours; 
    var min = Math.floor(dm/60000); 
    var ds = dm - 60000 * min; 
    var sec = Math.floor(ds/1000); 
    var dmilli = ds - 1000 * sec; 
    setTimeout(time, dmilli); 
    hours = ('0' + hours).slice(-2); 
    min = ('0' + min).slice(-2); 
    sec = ('0' + sec).slice(-2); 
    document.querySelector('#the-final-countdown p').innerHTML = hours + ':' + min + ':' + sec; 
} 
time();