JS:在页面刷新后,计时器继续运行

时间:2013-12-12 17:56:22

标签: javascript jquery

我想对其进行编辑,以便每次刷新页面时都不会更新计时器。

我搜索并且在我的情况下AJAX不是一个选项,因为我的代码页需要刷新,而计时器仍然是相同的。

另外,我试图将它保存在会话中,但这对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>

表格:

<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>

希望你能提供帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

将计时器的值存储在Cookie中并从该Cookie中提取,请在此处阅读Cookie:http://www.w3schools.com/js/js_cookies.asp