gpt4 book ai didi

javascript - JS : Timer continue even after page is refreshed

转载 作者:行者123 更新时间:2023-11-28 01:43:57 24 4
gpt4 key购买 nike

我想编辑此内容,以便计时器不会在每次刷新页面时更新。

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

此外,我尝试将其保存在 session 中,但这对 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>

希望能帮到你,谢谢。

最佳答案

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

关于javascript - JS : Timer continue even after page is refreshed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20550730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com