jquery - solution to countdown restarting after refreshing -
please kindly me countdown code restarting refresh page. also, minutes , seconds counting @ same time
below current javascript code;
function starttimer(duration, display) { var start = date.now(), diff, minutes, seconds; function timer() { // number of seconds have elapsed since // starttimer() called diff = duration - (((date.now() - start) / 1000) | 0); // same job parseint truncates float minutes = (diff / 60 * 60) | 0; seconds = (diff % 60) | 0; minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; display.textcontent = minutes + ":" + seconds; if (diff <= 0) { // add 1 second count down starts @ full duration // example 05:00 not 04:59 start = date.now() + 1000; } }; // don't want wait full second before timer starts timer(); setinterval(timer, 1000); } window.onload = function () { var fiveminutes = 60 * 2, display = document.queryselector('#time'); starttimer(fiveminutes, display); };
<div id="time"></div>
kindly me out........ in advance
here go. it's bit messy, in hurry, have tidy ;)
i used local storage store time , when refresh checks local storage , loads time that's left accordingly.
html:
<div id="time"></div>
js:
function countdown(minutes, seconds) { var currenttime; function twodigits(n) { return (n <= 9 ? '0' + n : n); } function updatetimer() { msleft = endtime - (+new date); time = new date(msleft); hours = time.getutchours(); mins = time.getutcminutes(); currenttime = (hours ? hours + ':' + twodigits(mins) : mins) + ':' + twodigits(time.getutcseconds()); localstorage.setitem('timer', currenttime); $('#time').text(currenttime); countdowntimer = settimeout(updatetimer, time.getutcmilliseconds() + 500); } endtime = (+new date) + 1000 * (60 * minutes + seconds) + 500; updatetimer(); } if (localstorage.getitem('timer')) { localtime = localstorage.getitem('timer'); var minutes = parseint(localtime.substr(0, localtime.indexof(':'))); var seconds = parseint(localtime = localtime.split(':')[1]); countdown(minutes, seconds); } else { countdown(120, 0); }
Comments
Post a Comment