Laravel jquery javascript localStorage - Stack Overflow

Problem is my timer was working as well. But when i save timers value to localStorage. I just want to w

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745388313a4625542.html

相关推荐

  • Laravel jquery javascript localStorage - Stack Overflow

    Problem is my timer was working as well. But when i save timers value to localStorage. I just want to w

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信