gpt4 book ai didi

javascript - 保存后 Cookie 的行为很奇怪

转载 作者:行者123 更新时间:2023-11-28 05:13:43 25 4
gpt4 key购买 nike

我正在尝试将倒计时保存为 cookie。我想制作上次倒计时的cookie,每次重新加载时都应该从上次倒计时开始。但我的代码表现得很奇怪。我不明白这是为什么。这是代码片段

window.onload = function() {
var a = document.cookie.split("=");
number = a[1];
}
var number = setInterval(thetimer,1000);


function thetimer() {

document.getElementById("demo3").innerHTML = number;
number += 1;
return number;
}


function alertf() {
var x;
if (confirm("Press any key?") == true) {
x = "you pressed ok";
} else{
x = "you pressed cancel";
}
document.getElementById("demo").innerHTML = x;
}

function getname() {
var x;
var name = prompt("Lets us know your name", "Chunnu Mia");
if(name == "") {
x = "What dont you telling??";
} else {
x = name+"!!! What a uselessname hahaha!!!";
}
document.getElementById("demo2").innerHTML = x;
}
document.cookie = "countdown="+number+";expires=Fri, 15 oct 2022 01:00:00 UTC";
<html>
<head>
<title>BOM</title>
</head>
<body>

<button onmouseover="alertf()">Dont touch me</button>
<button onmouseover="getname()">Whats your name?</button>
<p id="demo"></p>
<script src="index.js"></script>
<p id="demo2"></p>
<p id="demo3"></p>
<button onclick="clearInterval(timer)">Stop</button>
<p>01795923372</p>
</body>
</html>

我应该在这里改变什么?

最佳答案

number是一个区间的id,也是一个变量?

window.onload = function() {
var a = document.cookie.split("=");
number = Math.abs(a[1]); ///Just in case))
}
var number = 0;
var timer = setInterval(thetimer,1000);


function thetimer() {

document.getElementById("demo3").innerHTML = number;
number += 1;
///return number;

document.cookie = "countdown="+number+";expires=Fri, 15 oct 2022 01:00:00 UTC";

}

关于javascript - 保存后 Cookie 的行为很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41158785/

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