JQuery Cookie设置过期时间

时间:2013-02-18 15:44:09

标签: jquery cookies

我有这个:

if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 5 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();

如何让Cookie在10分钟后过期?

2 个答案:

答案 0 :(得分:18)

 var date = new Date();
 var m = 10;
 date.setTime(date.getTime() + (m * 60 * 1000));
 $.cookie("cookie", "value", { expires: date });

或者你可以使用一个函数:

function ExpireCookie(minutes) {
 var date = new Date();
 var m = minutes;
 date.setTime(date.getTime() + (m * 60 * 1000));
 $.cookie("cookie", "value", { expires: date });
}

然后执行ExpireCookie(10);

答案 1 :(得分:1)

10 minutes is 10 * 60 * 1000 milliseconds.

var date = new Date();
date.setTime(date.getTime() + (10 * 60 * 1000));
$.cookie("example", "foo", { expires: date });