javascript - Countdown don't stay run if close tabs -
i'm learning make countdown, , find guide on internet. when page closed countdown stops , if open page again countdown runs again, want make countdown continues run if page closed http://jsfiddle.net/fnsy4/
function setcookie(cname,cvalue,exdays) { var d = new date(); d.settime(d.gettime()+(exdays*24*60*60*1000)); var expires = "expires="+d.togmtstring(); document.cookie = cname + "=" + cvalue + "; " + expires; } function getcookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i=0; i<ca.length; i++) { var c = ca[i].trim(); if (c.indexof(name)==0) return c.substring(name.length,c.length); } return ""; } //check existing cookie cook=getcookie("my_cookie"); if(cook==""){ //cookie not found, set seconds=60 var seconds = 60; }else{ seconds = cook; console.log(cook); } function secondpassed() { var minutes = math.round((seconds - 30)/60); var remainingseconds = seconds % 60; if (remainingseconds < 10) { remainingseconds = "0" + remainingseconds; } //store seconds cookie setcookie("my_cookie",seconds,5); //here 5 expiry days document.getelementbyid('countdown').innerhtml = minutes + ":" + remainingseconds; if (seconds == 0) { clearinterval(countdowntimer); document.getelementbyid('countdown').innerhtml = "buzz buzz"; } else { seconds--; } } var countdowntimer = setinterval(secondpassed, 1000);
<span id="countdown" class="timer"></span>
if want countdown effective if tab closed, you'll have reconsider 2 (or more) options:
- you server side , countdown displayed in client window processed in server
- you set countdown given date (let's say) 16/12/2014 11:00:00 make calculus display. given date constant, no matter how many times countdown closed/opened, it'll true.
- once start countdown, save ending datetime cookie , restore countdown next time open page.
Comments
Post a Comment