猿问

js写出以下倒计时:当天时间倒计时到次日10点,次日之后每到0点开始倒计时到第二天的十点

小弟js比较弱,只能写出当前时间到给定时间的倒计时,求大牛解答,必将好好学习研究,谢谢。


呼啦一阵风
浏览 1401回答 3
3回答

慕桂英4014372

好似写的有点复杂。把倒计时时间,重置时间,计时器的偏差都加上了。var s = 1000,    m = 60 * s,    h = 60 * m,    d = 24 * h,    tick_time = 1000,    timezone = new Date().getTimezoneOffset() * m;var getDayTime = function(t) {  t = t.split(':');  return t[0] * h + t[1] * m;}var getNextClock = function(clockTime, resetTime, now) {  var result = now_d = parseInt(now / d, 10) * d + timezone;  now_d_t = now - now_d;  if (now_d_t > resetTime) {    result += d;  }  return result + clockTime;};var before = 0,  resetTime = getDayTime('00:00'),  clock24 = getDayTime('10:00');var tickHandler = function() {  var now = Date.now();  if (now - before > tick_time) {    var nextClock = getNextClock(clock24, resetTime, now);    var diff = nextClock - now;    var floor = Math.floor;    var diif_readable = [      floor(diff / d),      floor((diff % d) / h),      floor((diff % h) / m),      floor((diff % m) / s)    ];    console.log(diif_readable);    before = now;  }};var handler = setInterval(tickHandler, tick_time/10);tickHandler();
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答