【发布时间】:2022-11-25 04:33:28
【问题描述】:
我正在尝试将倒数计时器设置为 1 小时,当该小时结束时,计时器应将人们重定向到特定页面。
我当前的代码没有按预期工作,因为它没有在刷新页面后将倒计时存储在 localstorage 中。
<div id='stored'></div>
<script>
function countdown(minutes, seconds )
{
var endTime, hours, mins, msLeft, time;
function twoDigits( n )
{
return (n <= 9 ? '0' + n : n);
}
function updateTimer()
{
msLeft = endTime - (+new Date);
if ( msLeft < 1000 ) {
window.location.replace('done');
} else {
time = new Date( msLeft );
hours = time.getUTCHours();
mins = time.getUTCMinutes();
localStorage.setItem('timelol', (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() ));
document.getElementById('stored').innerHTML = localStorage.getItem('timelol');
setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
}
}
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
updateTimer();
}
countdown( 60,0 );
【问题讨论】:
-
有没有其他方法可以做上面提到的同样的事情?
标签: javascript