user3013190
user3013190

Reputation: 133

JS: Timer continue even after page is refreshed

I would like to edit this so that the timer won't be updated each time the page is refreshed.

I searched and AJAX in my case is not an option because my code pages need to be refreshed while the timer is still the same.

Also, I tried to save it in a session but that didn't work for JS.

JS:

  var mins
  var secs;

function cd() {
mins = 1 * m("5"); // change minutes here
secs = 0 + s(":01"); 
redo();
}

function m(obj) {
for(var i = 0; i < obj.length; i++) {
    if(obj.substring(i, i + 1) == ":")
    break;
}
return(obj.substring(0, i));
 }

  function s(obj) {
for(var i = 0; i < obj.length; i++) {
    if(obj.substring(i, i + 1) == ":")
    break;
}
return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs) {
    var disp;
    if(mins <= 9) {
        disp = " 0";
    } else {
        disp = " ";
    }
    disp += mins + " .";
    if(secs <= 9) {
        disp += "0" + secs;
    } else {
        disp += secs;
    }
    return(disp);
}

function redo() {
    secs--;
    if(secs == -1) {
        secs = 59;
        mins--;
    }
    document.cd.disp.value = dis(mins,secs); // setup additional displays here.
    if((mins == 0) && (secs == 0)) {
        window.alert(" Hey Time is up. Press OK to continue."); 
         window.location = "results_random.php" // redirects to specified page once timer ends and ok button is pressed
    } else {
        cd = setTimeout("redo()",1000);
    }
}

function init() {
  cd();
    }
     window.onload = init;
     </script>

The form:

<form name="cd">
 <label>
 <span class="labe">Remaining Time</span>:
 <input name="disp" type="text" class="clock" id="txt" value="10:00" size="10"      readonly="true" align="right" border="1" />
 <span class="labe">Minutes</span>
</form>

Hope you can help, thanks anyways.

Upvotes: 1

Views: 1732

Answers (1)

JustinM151
JustinM151

Reputation: 744

Store the value of the timer in a cookie and pull it from that cookie, read up on cookies here: http://www.w3schools.com/js/js_cookies.asp

Upvotes: 1

Related Questions