我正在尝试在javascript中制作一个倒数计时器,我写了一个像
这样的代码var countdown = function(){
setInterval(function() {
var countDownDate = new Date(document.getElementById("end_date").getAttribute("data-date")).getTime();
// data-date ex. = "2017-11-28 21:54:00"; greater than current date (now)
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
if(hours<10){
hours = "0"+hours;
}
if(minutes<10){
minutes = "0"+minutes;
}
if(seconds<10){
seconds = "0"+seconds;
}
var left = hours + ":"+ minutes + ":" + seconds;
console.log(left);
document.getElementById("time_left").innerHTML = left;
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("time_left").innerHTML = "EXPIRED";
}
}, 1000);
}
countdown();
计数器工作正常,但为什么我在不同系统上的时间差异达25秒。有些系统显示相同的倒计时时间,但有些则没有。
答案 0 :(得分:1)
你应该使用你的服务器时间和java脚本占用系统时间,所以当你改变你的系统时,倒计时会自动改变。你也可以使用js小提琴或其他任何东西来提供你的代码......