【发布时间】:2014-09-27 01:54:37
【问题描述】:
我正在尝试为我的网站设置一个 cookie,该 cookie 将在 3 分钟内到期,但没有运气......所以我找到了一个在 30 天内到期的代码,它非常有效,但我很难设置 3 分钟。
/**
* JS script with cookie integration for redirecting visitors to a splash page. jQuery free.
*
* @author Tyler Pearson
* @version 1.0
*/
var NMC = NMC || {};
NMC.Splash = (function () {
"use strict";
var daysBeforeCookieExpires = 30, //Need to change it for 3 min!!!
createCookie = function(name, value, expires, path, domain) {
var cookie = name + "=" + escape(value) + ";";
if (expires) {
if (expires instanceof Date) {
if (isNaN(expires.getTime())) {
expires = new Date();
}
} else {
expires = new Date(new Date().getTime() + parseInt(expires, 10) * 1000 * 60 * 60 * 24);
}
cookie += "expires=" + expires.toGMTString() + ";";
}
if (path) {
cookie += "path=" + path + ";";
}
if (domain) {
cookie += "domain=" + domain + ";";
}
document.cookie = cookie;
},
getCookie = function(name) {
var regexp = new RegExp("(?:^" + name + "|;\\s*" + name + ")=(.*?)(?:;|$)", "g"),
result = regexp.exec(document.cookie);
return (result === null) ? null : result[1];
},
readCookie = function(name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
i,
c;
for (i = 0; i < ca.length; i += 1) {
c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
};
return {
baseSetup: function(cookieName, splashURL) {
if (!(getCookie(cookieName))) {
createCookie("_splash-entrance", window.location.pathname, daysBeforeCookieExpires);
window.location = splashURL;
}
},
splashSetup: function(cookieName, expiresInDays) {
if (expiresInDays) {
daysBeforeCookieExpires = expiresInDays;
}
var link = document.getElementById('splash-continue');
createCookie(cookieName, true, daysBeforeCookieExpires);
if (!(readCookie("_splash-entrance") === null)) {
link.href = readCookie("_splash-entrance");
} else {
link.href = "/";
}
}
};
}());
【问题讨论】:
-
您认为需要更改这行代码的哪一部分?
expires = new Date(new Date().getTime() + parseInt(expires, 10) * 1000 * 60 * 60 * 24); -
我厌倦了更改那行代码,但它对我不起作用:expires = date.setTime(date.getTime() + (60 * 3000));