Javascript Cookie超时带倒计时器 [英] Javascript Cookie Timeout With Countdown Timer

查看:129
本文介绍了Javascript Cookie超时带倒计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javascript设置Cookie。足够简单。假设我设置了15分钟。

I want to set a cookie with javascript. Easy enough. Lets say I set it for 15 minutes.

我如何计算定时器显示以显示Cookie过期?即使他们离开了网页,我也希望它继续计数,当他们回到网页时,它仍然会倒计时。

How would I make a count down timer show to show when the cookie expires? And even if they left the page I would want it to keep counting and when they come back to the page it would still have been counting down.

对不起的解释。

感谢

推荐答案

如果没有cookie,请将现在+ 15分钟的时间戳存储在cookie中。
编写一个简单的脚本,每秒检查一次现在和时间戳之间的差异。

Store the time stamp of now + 15 minutes within a cookie if there is no cookie. Write a simple script that checks the difference between now and the timestamp every second.

编辑:
示例代码

sample code

// 200 seconds countdown
var countdown = 200; 

//current timestamp
var now   = Date.parse(new Date());

//ready should be stored in your cookie
var ready = Date.parse(new Date (now + countdown  * 1000)); // * 1000 to get ms


//every 1000 ms
setInterval(function()
{
    var sec = ( ready - Date.parse(new Date()) )/1000;
    document.title = sec + " seconds left";

},1000);

这篇关于Javascript Cookie超时带倒计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆