html5 - countdown timer using javascript should not restart on reload -


i have timer in checkout page, have 2 step checkout after step1 web page reloads step2 , hence timer restarts. need timer continue

here code

this javascript

 <input type="hidden" id="dealcost" name="dealcost" value="${model.dealcost}"/>  <script type="text/javascript">   var dealcst = document.getelementbyid('dealcost').value;    if(dealcst < 53){   zi_inceput1 = new date();   ceas_start1 = zi_inceput1.gettime();    function initstopwatch1() {       var timp_pe_pag1 = new date();       return ((timp_pe_pag1.gettime() + (1000 * 0) - ceas_start1) / 1000);   }   var tim = 1200;   function getsecs1() {       var tsecs1 = math.round(initstopwatch1());       if((tim-tsecs1)>=0)       {       var isecs1 = (tim-tsecs1) % 60;        var imins1 = math.round((tsecs1 - 30) / 60);       var ihour1 = math.round((imins1 - 30) / 60);       var imins1 = imins1 % 60;       var min = math.floor((tim-tsecs1) / 60);       if(min<10){          min = "0"+min;       }       var ihour1 = ihour1 % 24;       var ssecs1 = "" + ((isecs1 > 9) ? isecs1 : "0" + isecs1);       var smins1 = "" + ((imins1 > 9) ? imins1 : "0" + imins1);       var shour1 = "" + ((ihour1 > 9) ? ihour1 : "0" + ihour1);       document.getelementbyid('checkout_timer').innerhtml = min + ":" + ssecs1;       window.settimeout('getsecs1()', 1000);   }   else{       window.location.href="/deals";   }   }   window.settimeout('getsecs1()', 1000);   }  </script> 

this html code

 <c:if test='${model.dealcost < 53}'>  <div class="timer" style="float: right; width: 210px; font-size: 15px; margin-right: 20px;"> <div style="margin-top:20px;padding-bottom:5px;box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);background: white;overflow:none;">     <div class="otherheadercheckout"><div class="otherheadertextcheckout"><img src="/images/little_white_shopping_cart-19x19.png">&nbsp;timer</div></div>     <p align="center">time left checkout</p>     <div align="center" style="font-weight:600;">         <span id="checkout_timer"></span>     </div>  </div>   </div>  </c:if> 

if want save state of timer 1 page load next, have store timer information somewhere persists 1 page next. javascript variables not saved 1 page load next - reinitialized scratch each time page loads.

your options are:

  1. server-side: use ajax save timer state server can put subsequent page loads. troublesome because unless know right when user leaving page , can save remaining time server reliably @ moment, sort of timer save precision require lot of ajax calls server save time.

  2. client-side: store timer state locally via cookie or (in newer browsers) local storage. easy, can manipulated hacker (if care).


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -