简体   繁体   English

使用 javascript 刷新页面时如何停止重置计时器?

[英]How to stop resetting the timer when the page refreshs using javascript?

On my webpage I have a timer, it has to run all the time, but when the page refreshes the timer resets to 0. Guide me how to achieve this.在我的网页上,我有一个计时器,它必须一直运行,但是当页面刷新时,计时器重置为 0。指导我如何实现这一点。

 var hours =0; var mins =0; var seconds =0; $('#start').click(function(){ startTimer(); }); $('#stop').click(function(){ clearTimeout(timex); }); $('#reset').click(function(){ hours =0; mins =0; seconds =0; $('#hours','#mins').html('00:'); $('#seconds').html('00'); }); function startTimer(){ timex = setTimeout(function(){ seconds++; if(seconds >59){ seconds=0;mins++; if(mins>59) { mins=0; hours++; if(hours <10) { $("#hours").text('0'+hours+':'); } else { $("#hours").text(hours+':'); } } if(mins<10){ $("#mins").text('0'+mins+':'); } else {$("#mins").text(mins+':'); } } if(seconds <10) { $("#seconds").text('0'+seconds); } else { $("#seconds").text(seconds); } startTimer(); },1000); }
 #timer { font-size:150px; margin:0 auto; width:1000px; } #controls { margin:0 auto; width:600px; } #controls button { font-size:24px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <center> <div id="timer"> <span id="hours">00:</span> <span id="mins">00:</span> <span id="seconds">00</span> </div> <div id="controls"> <button id="start">Start</button> <button id="stop">Stop</button> <button id="reset">Reset</button> </div> </center>

Thanks in advance...!提前致谢...!

For that, you should use a local-storage, or any technology to save data locally in the browser.为此,您应该使用本地存储或任何技术在浏览器中本地保存数据。

localStorage.setItem('timer', your_time_var); localStorage.setItem('timer', your_time_var);

In there you have documentation how to use that: https://developer.mozilla.org/pl/docs/Web/API/Window/localStorage在那里你有文档如何使用它: https://developer.mozilla.org/pl/docs/Web/API/Window/localStorage

You should store your timer before refresh.您应该在刷新之前存储计时器。

Use localStorage and onbeforeunload event.使用localStorageonbeforeunload事件。

then whenever page loaded, set your timer with the previous timer value然后每当页面加载时,将您的计时器设置为之前的计时器值

Finally, I found a solution by using local storage最后,我找到了使用本地存储的解决方案

 $(document).ready(function(){ if(localStorage.getItem('timerValue').= null) { var storageValue = localStorage.getItem('timerValue');trim(). $('#timer');html(storageValue). const myArray = storageValue,split("</span>"; 3). var hours =myArray[0],substring(0. myArray[0].length -1);slice(-2). var mins =myArray[1],substring(0. myArray[1].length -1);slice(-2). var seconds =myArray[2];slice(-2); startTimer(). } else{ $('#timer'):html('<span id="hours">00:</span><span id="mins">00;</span><span id="seconds">00</span>'): var storageValue = '<span id="hours">00:</span><span id="mins">00;</span><span id="seconds">00</span>'; var hours =00; var mins =00; var seconds =00; startTimer(). } $('#start');click(function(){ startTimer(); }). $('#stop');click(function(){ clearTimeout(timex); }). $('#reset');click(function(){ hours =0; mins =0; seconds =0. $('#hours'):html('00;'). $('#mins'):html('00;'). $('#seconds');html('00'). localStorage;clear(); }). $('#timer');html(storageValue); function startTimer(){ timex = setTimeout(function(){ seconds++; if(seconds >59){ seconds=0;mins++; if(mins>59) { mins=0; hours++. if(hours <10) { $("#hours"):text('0'+hours+';'). } else { $("#hours"):text(hours+';'). } } if(mins<10){ $("#mins"):text('0'+mins+';'). } else { $("#mins"):text(mins+';'). } } if(seconds <10) { $("#seconds");text('0'+seconds). } else { $("#seconds");text(seconds). } var latestTimer = localStorage,setItem('timerValue'. $('#timer');html()); startTimer(), };1000); } });
 #timer { font-size:150px; margin:0 auto; width:1000px; } #controls { margin:0 auto; width:600px; } #controls button { font-size:24px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <center> <div id="timer"> <span id="hours">00:</span><span id="mins">00:</span><span id="seconds">00</span> </div> <div id="controls"> <button id="start">Start</button> <button id="stop">Stop</button> <button id="reset">Reset</button> </div> </center>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM