jQuery cookie 过期值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3417087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery cookie expiration value
提问by Chris
I have read a lot of jQuery cookie questions on here and know there is a jQuery cookie plugin (jQuery cookie). Without doing much investigation, the question: is there a way to determine expiration date of cookie?
我在这里阅读了很多 jQuery cookie 问题,并且知道有一个 jQuery cookie 插件 ( jQuery cookie)。没有做太多调查,问题是:有没有办法确定 cookie 的到期日期?
From the jquery.cookie doc:
从 jquery.cookie 文档:
/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/[email protected]
*/
Doesn't seem that this plugin can do it?
这个插件好像不行啊?
The reason I want to do this is that my cookie expires after 5 minutes of inactivity and Id like to notify user that their session is about to expire from Javascript.
我想这样做的原因是我的 cookie 在 5 分钟不活动后过期,我想通知用户他们的会话即将从 Javascript 过期。
采纳答案by Nick Craver
Unless something has changed, you can't get this value froma cookie, you can set it but that's it, when it expires it just won't show up in the cookie collection anymore....but you can't see that it expires in 5 minutes for example.
除非某些事情发生了变化,否则您无法从cookie 中获取此值,您可以设置它,但仅此而已,当它过期时,它将不再出现在 cookie 集合中......但你看不到例如,它会在 5 分钟后过期。
Your best bet for something along the lines of session expiration is to use setTimeout()
with the correct delay, for example if it's 5 minutes, you may want to alert at 4 min 30 seconds, like this:
您最好的选择是使用setTimeout()
正确的延迟,例如,如果是 5 分钟,您可能希望在 4 分 30 秒时发出警报,如下所示:
setTimeout(function() {
alert("Your session will expire in 30 seconds!");
}, 270000); //4.5 * 60 * 1000
回答by Sotiris
$.cookie("example", "foo", { expires: 7 });
Will expire in 7 days
将在 7 天后到期
there is no Javascript API that allows you to check the expiry date for a cookie
没有允许您检查 cookie 的到期日期的 Javascript API
回答by SF.
As it's not accessible from the JavaScript API, the only way is to store it in the content in parallel with the metadata.
由于它不能从 JavaScript API 访问,唯一的方法是将它与元数据并行存储在内容中。
var textContent = "xxxx"
var expireDays = 10;
var now = new Date().getTime();
var expireDate = now + (1000*60*60*24*expireDays);
$.cookie("myCookie", '{"data": "'+ textContent +'", "expires": '+ expireDate +'}', { expires: expireDays });
Then read it back (obviously, add safeguards in case the cookie expired already) :
然后读回它(显然,添加保护措施以防 cookie 已经过期):
var now = new Date().getTime();
var cookie = $.parseJSON($.cookie("myCookie"));
var timeleft = cookie.expires - now;
var cookieData = cookie.data;
Note this will not be entirely reliable if the client clock changes in the meantime, say, due to DST.
请注意,如果客户端时钟在此期间发生变化(例如,由于 DST),这将不完全可靠。