代码:
<div class="readTiming">
<time>00:00:00</time><br/>
</div>
<input type="hidden" name="readTime" id="readTime">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var p = document.getElementsByClassName('readTiming')[0],
seconds = 0, minutes = 0, hours = 0,
t;
function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
}
p.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
timer();
localStorage.setItem("timeStore", p.textContent);
getElementsByClassName('readTiming').innerHTML = localStorage.getItem("timeStore");
$("#readTime").val(p.textContent);
}
function timer() {
t = setTimeout(add, 1000);
}
timer();
</script>
在这段代码中,我有一个计时器。现在,我希望如果用户刷新页面,那么它不会改变并且计时器时间将继续,如果用户按下浏览器后退按钮,那么它将节省退出时间。那么,我该怎么做呢?请帮我。
人到中年有点甜
阿波罗的战车