[英]How to set Local Storage on Timer
When I refresh page timer is reset.当我刷新页面计时器被重置。 I would like to set localStorage on this timer, how do it set in localStorage
我想在这个计时器上设置 localStorage,如何在 localStorage 中设置
HTML HTML
00 : <span id="timer">02 : 00</span>
JavaScript :- JavaScript :-
window.onload = function() {
var min = "0" + 2;
var sec = "0" + 0;
setInterval(function() {
var a = new Date();
var time = min + " : " + sec;
document.getElementById("timer").innerHTML = time;
if (sec == 00) {
if (min != 0) {
min--;
sec = 59;
if (min < 10) {
min = "0" + min;
}
}
} else {
sec--;
if (sec < 10) {
sec = "0" + sec;
}
}
}, 1000);
}
You can set Local storage like this:您可以像这样设置本地存储:
1- On windows load
check if you have already local storage for variables min
and sec
, if yes get both values using localStorage.getItem("item")
else set both variables to start values. 1- 在 Windows
load
检查您是否已经有变量min
和sec
本地存储,如果是,则使用localStorage.getItem("item")
获取两个值,否则将两个变量都设置为起始值。
2- Inside setInterval
make sure that you are updating both local storage for min
and sec
while updating those variables. 2- 在
setInterval
确保在更新这些变量的同时更新min
和sec
本地存储。
You can use HTML5 local storage feature.您可以使用 HTML5 本地存储功能。
<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.