gpt4 book ai didi

javascript - 如何让这个 cookie 在 14 天后过期

转载 作者:数据小太阳 更新时间:2023-10-29 05:51:55 24 4
gpt4 key购买 nike

function setcookie(cookieName, cookieValue, nDays) {
var today = new Date();
var expire = new Date();
if (nDays == null || nDays == 0) nDays = 1;
expire.setTime(today.getTime() + 3600000 * 24 * nDays); // changed that to * 14
document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

function readcookie(cookieName) {
var theCookie = " " + document.cookie;
var ind = theCookie.indexOf(" " + cookieName + "=");
if (ind == -1) ind = theCookie.indexOf(";" + cookieName + "=");
if (ind == -1 || cookieName == "") return "";
var ind1 = theCookie.indexOf(";", ind + 1);
if (ind1 == -1) ind1 = theCookie.length;
return unescape(theCookie.substring(ind + cookieName.length + 2, ind1));

}

我尝试将 nday 更改为 nday=14 但没有任何效果。

然后我尝试了 expire.setTime(today.getTime() + 3600000 * 24 * 14) ,还是什么都没有

我只需要使用这段代码让 cookie 在设定的天数后过期。抱歉,我是本周刚开始接触 javascript 的新手。

最佳答案

如果你一直想设置你的cookies为14天,删除你的nDays参数,直接在expire.setTime方法中设置14天

function setcookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + 3600000*24*14);
document.cookie = cookieName+"="+encodeURI(cookieValue) + ";expires="+expire.toGMTString();
}

关于javascript - 如何让这个 cookie 在 14 天后过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32497923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com