这是我的代码,其中我使用了readCookies函数,但是当它没有响应时,它在div中没有显示任何内容,即使警报也无效。
seconds = readCookie('countdown') || 60;
alert(seconds);
function secondPassed() {
seconds--;
var minutes = parseInt(seconds / 60);
var remainingSeconds = parseInt(seconds % 60);
if (remainingSeconds < 10) {
remainingSeconds = "0" + parseInt(remainingSeconds);
}
document.getElementById('countdown').innerHTML = minutes + " mins : " + remainingSeconds + " secs";
if (parseInt(seconds) === 0) {
alert("Time is over");
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "Time is Over";
document.forms["myForm"].submit();
document.location.href = "examresult.jsp";
}
}
var countdownTimer = setInterval(function () {
secondPassed();
if (seconds === 0) {
eraseCookie(seconds);
} else {
createCookie(seconds, seconds, 7);
}
}, 1000);
答案 0 :(得分:1)
在页面卸载之前将值存储在cookie或浏览器存储中,并在页面刷新时从cookie或浏览器存储中重新加载。
例如(伪代码):
window.onunload = function(){
set_cookie('timer', timer) /* you need to implement this function */;
}
window.onload = function() {
timer = get_cookie('timer'); /* you need to implement this function */
if ( null == timer ) timer = 0; /* not page refresh, start a new timer */
}