admin管理员组

文章数量:1426052

Problem is my timer was working as well. But when i save timers value to localStorage. I just want to when user refresh timer wont stop and resume when stopped at.

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;
}

in my view

<h3 onload="Dahin();" class="page-title">Шалгалтын 50 асуулт</h3><h4><div>Үлдсэн хугацаа = <span id="timer">{{ $time }}</span></div></h4>

Update fixed timer

now how to save timer value to sessionstorage and retrieve when refresh page

Problem is my timer was working as well. But when i save timers value to localStorage. I just want to when user refresh timer wont stop and resume when stopped at.

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;
}

in my view

<h3 onload="Dahin();" class="page-title">Шалгалтын 50 асуулт</h3><h4><div>Үлдсэн хугацаа = <span id="timer">{{ $time }}</span></div></h4>

Update fixed timer

now how to save timer value to sessionstorage and retrieve when refresh page

Share Improve this question edited Apr 12, 2018 at 2:23 Ruka Xing asked Apr 12, 2018 at 2:13 Ruka XingRuka Xing 6421 gold badge13 silver badges30 bronze badges 7
  • 1 is your timer time is same with your new Date() time? – Jay Commented Apr 12, 2018 at 3:57
  • No its not. <span id="timer">30:00</span> its being like this. Problem is how to save timer value to localstorage – Ruka Xing Commented Apr 12, 2018 at 4:05
  • when you use alert(startTime). what does it return? – Jay Commented Apr 12, 2018 at 4:14
  • null, null, null each second – Ruka Xing Commented Apr 12, 2018 at 4:15
  • what is this? var presentTime = document.getElementById('timer').innerHTML;. I think this variable will return html tag.. what do you expect from this variable? – Jay Commented Apr 12, 2018 at 4:22
 |  Show 2 more ments

1 Answer 1

Reset to default 3

Let change your code:

var check = localStorage.getItem("startTime");
var startTime = check ? check : document.getElementById("timer").innerHTML;
document.getElementById('timer').value = startTime;

function Dahin(){
    localStorage.setItem("startTime", startTime);
}

the problem why you get null is because you use .value. You put your time into span tag. .value is only work for input field

本文标签: Laravel jquery javascript localStorageStack Overflow