我想制作一个从几小时而不是几天倒计时的JavaScript时钟。我已经使用了一个很好的countdown JS script from Stackoverflow,我正在尝试使代码适应我的喜好。JavaScript时钟,只倒计时小时,分钟和秒..不是天
我的代码:
// CLOCK
var end = new Date(2017,4,22,23,59);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function clock() {
var now = new Date();
var distance = end - now;
var days = Math.floor(distance/_day);
// get the total day number example, 3 days
// times that by 3, so 3x24 = 72 hours
var plus = days * 24;
// no make the hours + the days, so 4 hours + 72 etc
var hours = Math.floor((distance % _day)/_hour + plus);
var minutes = Math.floor((distance % _hour)/_minute);
var seconds = Math.floor((distance % _minute)/_second);
console.log('Quick! Get the sale while it lasts')
console.log(+hours + ' hours remaining..');
console.log(+seconds + ' seconds remaining..');
console.log(+minutes + ' mins remaining..');
}
var countdownTimer = setInterval('clock()', 1000);
如果今天是五月18日,结束日期是20,那么倒计时应该在48小时但它不能正常工作。似乎正在使用旧的2天和15个小时..我不想与天有关。我想要的是几小时而不是几天。
任何指针?
嘛,拿出了'days' ...的小时数仅仅是'距离/ _hour'计算。 – deceze