如何在 Jquery 中将 cookie 设置为在 1 分钟或 30 秒后过期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22371826/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:55:12  来源:igfitidea点击:

How do I set a cookie to expire after 1 minute or 30 seconds in Jquery?

jquerycookiesjquery-cookie

提问by Attila Naghi

How do i set my cookie to expire after 30 sec or 1 m ? this is my code :

我如何设置我的 cookie 在 30 秒或 1 m 后过期?这是我的代码:

$.cookie('username', username, { expires: 14 });  // expires after 14 days

回答by Felix

For 1 minute, you can use:

1分钟,您可以使用:

var date = new Date();
date.setTime(date.getTime() + (60 * 1000));
$.cookie('username', username, { expires: date });  // expires after 1 minute

For 30 seconds, you can use:

30 秒内,您可以使用:

var date = new Date();
date.setTime(date.getTime() + (30 * 1000));
$.cookie('username', username, { expires: date });  // expires after 30 second

回答by Sebastian Norr

Source: http://www.informit.com/articles/article.aspx?p=24592&seqNum=3

来源:http: //www.informit.com/articles/article.aspx?p=24592& seqNum=3

Quote:

引用:

You need to create the expiration date in seconds—not only that, but it has to be in seconds since January 1, 1970. You may wonder how you are going to figure out your expiration dates when you have to determine them in regard to January 1, 1970. This is where the time() function comes in.

您需要以秒为单位创建到期日期 - 不仅如此,而且它必须是自 1970 年 1 月 1 日以来的秒数。您可能想知道当您必须确定与 1 月相关的到期日期时,您将如何计算它们1, 1970. 这就是 time() 函数的用武之地。

The time() function returns the number of seconds since January 1, 1970. If you want to create a cookie that expires in 30 days, you need to do the following:

time() 函数返回自 1970 年 1 月 1 日以来的秒数。 如果要创建 30 天后过期的 cookie,则需要执行以下操作:

  • Get the number of seconds since 1970.

  • Determine the number of seconds that you want the cookie to last.

  • Add the number of seconds since 1970 to the number of seconds that you want the cookie to last.

  • 获取自 1970 年以来的秒数。

  • 确定您希望 cookie 持续的秒数。

  • 将自 1970 年以来的秒数与您希望 cookie 持续的秒数相加。

Because we know that there are 86,400 seconds in a day (60 seconds x 60 minutes x 24 hours), you could create a cookie that expires in 30 days, like this:

因为我们知道一天有 86,400 秒(60 秒 x 60 分钟 x 24 小时),您可以创建一个 30 天后过期的 cookie,如下所示:

setcookie("username", "chris", time() + (86400 * 30));

This function places a cookie on the user's browser for 30 days. Anytime during those 30 days, you can access the variable $username from within the script and it will return (in the above example) chris.

此函数在用户的浏览器上放置 30 天的 cookie。在这 30 天内的任何时候,您都可以从脚本中访问变量 $username,它会返回(在上面的示例中)chris。

回答by Tushar Gupta - curioustushar

var date = new Date();
date.setTime(date.getTime() + (30 * 1000)); //add 30s to current date-time 1s = 1000ms
$.cookie('username', username, { expires: date });  //set it expiry

回答by Butani Vijay

You can Use as below for 1 minute and 30 seconds:

您可以如下使用1 minute and 30 seconds

 var date = new Date();
 var minutes = 1.5;
 date.setTime(date.getTime() + (minutes * 60 * 1000));
 $.cookie('username', username, { expires: date });

//3.5* 60 * 1000 = 1 minute and 30 seconds

//For 30 Seconds

//30秒

  var date = new Date();
 var minutes = 0.5;
 date.setTime(date.getTime() + (minutes * 60 * 1000));
 $.cookie('username', username, { expires: date });