dongmeba4877
2015-05-28 05:58
浏览 874
已采纳

倒计时器再次重新加载刷新

countdown timer code in javascript. But it is restarting again when the page is refreshed. I want the timer should continue even though it is restarted.

Help me to deal with this problem as i am beginner this is my first application i need some detail inputs from you.

`
<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>
 `

图片转代码服务由CSDN问答提供 功能建议

javascript中的倒数计时器代码。 但是当页面刷新时它会再次重新启动。 我希望计时器应该继续,即使它重新启动。

帮助我处理这个问题,因为我是初学者这是我的第一个应用程序,我需要你的一些细节输入。

 `
&lt; body&gt; 
&lt; div id =“divCounter”&gt;&lt; / div&gt; 
&lt; script type =“text / javascript”&gt; 
 
var  hoursleft = 0; 
var minutesleft = 35;  //你可以将这些值更改为任何大于0的值
var secondsleft = 0; 
 
var finishedtext =“倒计时结束!”  //倒计时到达0时出现的文本
 
ndar = new Date(); 
 
end.setMinutes(end.getMinutes()+ minutesleft); 
end.setSeconds(end.getSeconds()+ secondsleft); \  n 
 
if(localStorage.getItem(“counter”)){
 
 var value = localStorage.getItem(“counter”); 
 
} else {
 var value = 0; 
} \  n 
 
var counter = function(){
 
 
 var now now = new Date(); 
var diff = end  -  now; 
diff = new Date(diff); 
 
var sec = diff。  getSeconds(); 
var min = diff.getMinutes(); 
 
if(min&lt; 10){
 min =“0”+ min; 
} 
if(sec&lt; 10){
 sec  =“0”+ sec; 
} 
 
if(现在&gt; =结束){
 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')附加(值); 
document。  getElementById('divCounter')。innerHTML = value;  
} 
 
 
 
 
 \ var var interval = setInterval(function(){counter();},1000); 
&lt; / script&gt; 
&lt; / body&gt; 
  
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duanpang2751 2015-06-02 08:06
    已采纳

    countdown timer for whatever time you want. It does not restart when you refresh the page.

    <body>
    <div id="divCounter"></div>
    
    
    <script>
    //var hoursleft = 0;
    var minutesleft = 0; //give minutes you wish
    var secondsleft = 30; // give seconds you wish
    var finishedtext = "Countdown finished!";
    var end1;
    if(localStorage.getItem("end1")) {
    end1 = new Date(localStorage.getItem("end1"));
    } else {
    end1 = new Date();
    end1.setMinutes(end1.getMinutes()+minutesleft);
    end1.setSeconds(end1.getSeconds()+secondsleft);
    
    }
    var counter = function () {
    var now = new Date();
    var diff = end1 - now;
    
    diff = new Date(diff);
    
    var milliseconds = parseInt((diff%1000)/100)
        var sec = parseInt((diff/1000)%60)
        var mins = parseInt((diff/(1000*60))%60)
        //var hours = parseInt((diff/(1000*60*60))%24);
    
    if (mins < 10) {
        mins = "0" + mins;
    }
    if (sec < 10) { 
        sec = "0" + sec;
    }     
    if(now >= end1) {     
        clearTimeout(interval);
       // localStorage.setItem("end", null);
         localStorage.removeItem("end1");
         localStorage.clear();
        document.getElementById('divCounter').innerHTML = finishedtext;
         if(confirm("TIME UP!"))
         window.location.href= "timeup.php";
    } else {
        var value = mins + ":" + sec;
        localStorage.setItem("end1", end1);
        document.getElementById('divCounter').innerHTML = value;
    }
    }
    var interval = setInterval(counter, 1000);
    </script>
    </body>
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(4条)

相关推荐 更多相似问题