我有一个javascript倒计时脚本,我希望我的页面在倒计时结束时刷新一次.
这是我的完整代码.
//JavaScript code for countdown function getTimeRemaining(endtime) { var t = Date.parse(endtime) - Date.now(); 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); if (t.total < 0) { document.getElementById("clockdiv").className = "hidden-div"; document.getElementById("timeIsNow").className = "visible-div"; clearInterval(timeinterval); return true; } 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); } updateClock(); var timeinterval = setInterval(updateClock, 1000); } var deadline = '2015-12-31T13:00:00+02:00'; console.log(deadline); initializeClock('clockdiv', deadline);
这是我想用于刷新的代码:
location.reload();
如果我在下面这段代码中添加这个刷新代码,刷新工作.
if (t.total < 0) { document.getElementById("clockdiv").className = "hidden-div"; document.getElementById("timeIsNow").className = "visible-div"; clearInterval(timeinterval); location.reload(); //Added this one. return true; }
但是,这种方式在倒计时结束后每秒刷新一次.但我希望它只刷新一次,当它达到0时.
我尝试了什么?
我尝试创建另一个if语句来刷新页面t.total == 0
,就像这样:
if (t.total == 0) { document.getElementById("clockdiv").className = "hidden-div"; document.getElementById("timeIsNow").className = "visible-div"; clearInterval(timeinterval); location.reload(); //Added this one. return true; }
但不知何故,它没有用.
所以我也试过了,if (t.total = 0)
如果(t.total === 0)
但仍然无法解决它.
当计数器达到0时,有人可以帮我刷新页面吗?
如果你想玩,这是一个jsFiddle:https://jsfiddle.net/qv6rbyLo/1/