1
我想这是我的JavaScript代码能够被读取3小时倒计时,并重定向到倒计时后,一个新的页面完成JavaScript的倒计时附和错误的时间
<script type="text/javascript">
// properties
var count = 0;
var counter = null;
window.onload = function() {
initCounter();
};
function initCounter() {
// get count from localStorage, or set to initial value of 1000
count = getLocalStorage('count') || 1000;
counter = setInterval(timer, 1000); //1000 will run it every 1 second
}
function setLocalStorage(key, val) {
if (window.localStorage) {
window.localStorage.setItem(key, val);
}
return val;
}
function getLocalStorage(key) {
return window.localStorage ? window.localStorage.getItem(key) : '';
}
function timer() {
count = setLocalStorage('count', count - 1);
if (count == -1) {
clearInterval(counter);
return;
}
var seconds = count % 60;
var minutes = Math.floor(count/60);
var hours = Math.floor(minutes/60);
minutes %= 60;
hours %= 60;
document.getElementById("timer").innerHTML = hours + "hours " + minutes + "minutes and " + seconds + " seconds left to complete this transaction"; // watch for spelling
}
</script>
<div id="timer"></div>
请帮助我变得更好用使其倒计时到三小时,倒计时完成后又重定向到另一页
我只是想让你帮我检查一下我将在三个小时内完成任务而不改变我的代码。 – phemieny7
@ phemieny7。 'count = getLocalStorage('count')|| 3600 * 3'而不是'count = getLocalStorage('count')|| 1000'。但我**强烈**推荐你不要在这种情况下使用'setInterval'和'setTimeout'函数。 – user7771338
是的,我爱你的代码,但我得到的问题是,它重新启动时,页面刷新。如果你能纠正这一点,我真的很喜欢使用它。请帮我纠正一下。谢谢。 – phemieny7