我有一个运行良好的javascript计时器。但是我希望它即使在重新加载时也能保持其状态。正如此处How to keep my javascript timer from restarting on page reload所述,由于我是JavaScript新手,因此代码很难阅读,因此我不得不根据自己的知识提出一个新问题
这是我的下面的代码
var total_seconds = 60 * 20;
var c_minutes = parseInt(total_seconds / 60);
var c_seconds = parseInt(total_seconds % 60);
var stud = "StudId";
var csid = "sId";
function CheckTime() {
document.getElementById("quiz-time-left").innerHTML = 'Time Left: ' + c_minutes + ' minutes ' + c_seconds + ' seconds';
if (total_seconds <= 0) {
setTimeout(window.location.href = "discard.php?stud=" + stud + "&csid=" + csid, 1);
} else {
total_seconds = total_seconds - 1;
c_minutes = parseInt(total_seconds / 60);
c_seconds = parseInt(total_seconds % 60);
setTimeout("CheckTime()", 1000);
}
}
setTimeout("CheckTime()", 1000);
<span id="quiz-time-left"></span>