JavaScript中的倒計時器代碼。但是當頁面刷新時它會重新啓動。我希望計時器應該繼續,即使它重新啓動。倒數計時器在刷新時重新加載
幫我解決這個問題,因爲我是初學者這是我的第一個應用程序,我需要一些來自你的細節輸入。
`
<body>
<div id="divCounter"></div>
<script type="text/javascript">
var hoursleft = 0;
var minutesleft = 35; // you can change these values to any value greater than 0
var secondsleft = 0;
var finishedtext = "Countdown finished!" // text that appears when the countdown reaches 0
var end = new Date();
end.setMinutes(end.getMinutes()+minutesleft);
end.setSeconds(end.getSeconds()+secondsleft);
if(localStorage.getItem("counter")){
var value = localStorage.getItem("counter");
}else{
var value = 0;
}
var counter = function(){
var now = new Date();
var diff = end - now;
diff = new Date(diff);
var sec = diff.getSeconds();
var min = diff.getMinutes();
if (min < 10){
min = "0" + min;
}
if (sec < 10){
sec = "0" + sec;
}
if(now >= end){
clearTimeout(timerID);
document.getElementById('divCounter').innerHTML = finishedtext;
}
else{
var value = min + ":" + sec;
//document.getElementById('divCounter').innerHTML = min + ":" + sec;
localStorage.setItem("counter", JSON.stringify(value));
}
// timerID = setTimeout("cd()", 1000);
// value = JSON.parse(localStorage.getItem("counter"));
//$('#divCounter').append(value);
document.getElementById('divCounter').innerHTML = value;
}
var interval = setInterval(function(){counter();}, 1000);
</script>
</body>
`
如何將這些值存儲在內存中,並在刷新頁面時加載它們。請解釋我,否則給鏈接,我會從那裏學習 –
@SMadhu我上傳了一個新的答案 - 你可以從那裏複製代碼 – kabiroberai
謝謝你正在工作,但是當我想再次嘗試它而不是時間我越來越「楠:NAN」。我怎樣才能重置計時器? –