问题是我的计时器
也正常工作。但是当我将timer
的value
保存到localStorage时。我只是想当用户刷新 timer
时不会停止并在停止时恢复。
javascript
function startTimer() {
var presentTime = document.getElementById('timer').innerHTML;
var timeArray = presentTime.split(/[:]+/);
var m = timeArray[0];
var s = checkSecond((timeArray[1] - 1));
if(s==59){m=m-1}
if(m<0){ document.myform.submit(); }
document.getElementById('timer').innerHTML =
m + ":" + s;
setTimeout(startTimer, 1000);
var startTime = document.getElementById("timer").value;
localStorage.setItem("startTime", startTime);
//alert(startTime);
}
function Dahin(){
var startTime = localStorage.getItem("startTime");
document.getElementById('timer').value = startTime;
}
在我看来
<h3 onload="Dahin();" class="page-title">Шалгалтын 50 асуулт</h3><h4><div>Үлдсэн хугацаа = <span id="timer">{{ $time }}</span></div></h4>
更新固定计时器
现在如何将计时器值保存到 session 存储并在刷新页面时检索
最佳答案
让我们更改您的代码:
var check = localStorage.getItem("startTime");
var startTime = check ? check : document.getElementById("timer").innerHTML;
document.getElementById('timer').value = startTime;
function Dahin(){
localStorage.setItem("startTime", startTime);
}
为什么你得到null的问题是因为你使用了.value
。你把你的时间放入span标签中。 .value
仅适用于输入字段
关于javascript - Laravel jquery javascript localStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49786938/