Javascript Cookie超时带倒计时器

编程入门 行业动态 更新时间:2024-10-12 05:51:34
本文介绍了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超时带倒计时器

本文发布于:2023-11-28 12:01:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   Javascript   Cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!