Javascript 存在cookie问题的JS倒计时
我们有24小时倒计时。问题是,每当刷新页面时,计时器就会重新启动。我们如何创建cookie,使其在刷新时不会为同一用户重新启动?如果下降到0,会重新启动吗 到目前为止我们拥有的:Javascript 存在cookie问题的JS倒计时,javascript,counter,countdown,Javascript,Counter,Countdown,我们有24小时倒计时。问题是,每当刷新页面时,计时器就会重新启动。我们如何创建cookie,使其在刷新时不会为同一用户重新启动?如果下降到0,会重新启动吗 到目前为止我们拥有的: <script type = "text/javascript"> var totalSeconds; function initiate(seconds) { totalSeconds = parseInt(seconds); setInterval("timeUpdate()", 1000);
<script type = "text/javascript">
var totalSeconds;
function initiate(seconds)
{
totalSeconds = parseInt(seconds);
setInterval("timeUpdate()", 1000);
}
function timeUpdate()
{
var seconds = totalSeconds;
if(seconds > 0)
{
totalSeconds--;
var hours= Math.floor(seconds/3600);
seconds %= 3600;
var minutes = Math.floor(seconds/60);
seconds %= 60;
var timeIs = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + seconds;
document.getElementById("timeLeft").innerHTML = "" + timeIs;
}
else
{
document.getElementById("timeLeft").innerHTML = '';
document.getElementById("message").innerHTML = '';
}
}
initiate(24 * 60 * 60);
</script>
var总秒数;
功能启动(秒)
{
totalSeconds=parseInt(秒);
setInterval(“timeUpdate()”,1000);
}
函数timeUpdate()
{
var秒=总秒数;
如果(秒>0)
{
总秒--;
var小时=数学地板(秒/3600);
秒%=3600;
var分钟=数学地板(秒/60);
秒%=60;
var timeIs=((小时<10)?“0”:“)+小时+”:“+((分钟<10)?“0”:“)+分钟+”:“+((秒<10)?“0”:“)+秒;
document.getElementById(“timeLeft”).innerHTML=”“+timeIs;
}
其他的
{
document.getElementById(“timeLeft”).innerHTML='';
document.getElementById(“消息”).innerHTML='';
}
}
发起(24*60*60);
首先,我们需要具有设置和读取cookie的功能。为此,请使用本答案中给出的函数 因此,我们的代码中有两个函数
createCookie
和setCookie
现在在页面加载时设置并获取cookie,如下所示
var
//Get time started
timeStarted = getCookie('timeStarted'),
//To store total seconds left
totalSeconds,
//Current Time
currentTime = parseInt(new Date()/1000),
//Timer Length
timerLength = 24 * 60 * 60;
if(timeStarted == "") {
//Time not yet started. Start now.
createCookie('timeStarted', currentTime, 365);
//We started time just now. So, we have full timer length remaining
totalSeconds = timerLength;
} else {
//Calculate total seconds remaining
totalSeconds = timerLength - (currentTime - timeStarted)%(timerLength);
}
initialize(totalSeconds);
现在初始化如下所示
var
//Get time started
timeStarted = getCookie('timeStarted'),
//To store total seconds left
totalSeconds,
//Current Time
currentTime = parseInt(new Date()/1000),
//Timer Length
timerLength = 24 * 60 * 60;
if(timeStarted == "") {
//Time not yet started. Start now.
createCookie('timeStarted', currentTime, 365);
//We started time just now. So, we have full timer length remaining
totalSeconds = timerLength;
} else {
//Calculate total seconds remaining
totalSeconds = timerLength - (currentTime - timeStarted)%(timerLength);
}
initialize(totalSeconds);
您的两个函数都可以正常工作并保持不变
我用365天作为cookie周期。我们只需要开始时间
根据您的要求更改代码
如果您需要有关此代码的任何说明,请告诉我。谢谢,但我是这方面的新手。我将初始化(totalSeconds)放在何处;准确地说?我尝试添加第一段代码和cookie代码,并按照上面所述进行设置和获取,然后body onload initialize(totalSeconds);但计时器会在页面刷新时重新启动。请您按照我应该使用的方式把所有代码块放在一起,好吗?谢谢你的帮助!希望您正在从服务器运行该文件,并且浏览器中已启用Cookie。从服务器运行的文件只能与cookie一起播放。您可以设置初始化(总秒);最后。所以,它是这样的,所有的函数首先出现,然后是给定的代码,然后是初始化。我该如何使用它?哪里你能展示一下如何把这些代码放在页面上吗?谢谢你的帮助!您可以在链接中看到一个示例