以下是我的javascript代码。每当页面重新加载时,它会重置为27天23小时59分59秒。
我会做什么改变,每次页面重新加载后都不会刷新?
<script type="text/javascript">
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var daysSpan = clock.querySelector('.days');
var hoursSpan = clock.querySelector('.hours');
var minutesSpan = clock.querySelector('.minutes');
var secondsSpan = clock.querySelector('.seconds');
function updateClock() {
var t = getTimeRemaining(endtime);
daysSpan.innerHTML = t.days;
hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeinterval);
}
}
updateClock();
var timeinterval = setInterval(updateClock, 1000);
}
var deadline = new Date(Date.parse(new Date()) + 28 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);
</script>
答案 0 :(得分:2)
var deadline = new Date(Date.parse(new Date()) + 28 * 24 * 60 * 60 * 1000);
您正在从这个确切时刻创建日期,然后再添加28天。您应该改为定义目标日期。
var deadline = new Date(year, month, day, hours, minutes, seconds, milliseconds);
//To find the remaining time until new Year you'd do
var deadline = new Date(2018, 0, 1);
月份是0到11,11是12月。阅读有关日期对象https://www.w3schools.com/js/js_dates.asp
的更多信息