3 回答
TA贡献1871条经验 获得超13个赞
好似写的有点复杂。把倒计时时间,重置时间,计时器的偏差都加上了。
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();
添加回答
举报