javascript - Cookie 一小时后就会过期吗?

标签 javascript cookies

我捏了下面的 JavaScript 代码,将 cookie 应用到文档就绪时弹出的模式框。如何调整以下内容以使 cookie 只持续一个小时?我认为此刻它会永远持续下去?

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);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function getCookie(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));
}

    $(document).ready(function() {

        var skipModal = getCookie('skipModal');
     if (!skipModal) { // check and see if a cookie exists indicating we should skip the modal
         $('#myModal').reveal({
     animation: 'fadeAndPop',                   //fade, fadeAndPop, none
     animationspeed: 300,                       //how fast animtions are
     closeonbackgroundclick: true,              //if you click background will modal close?
     dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
    });


         setCookie('skipModal', 'true', 365*5); // set a cookie indicating we should skip the modal
     }
});

最佳答案

Javascript 时间以毫秒为单位,因此请更改

expire.setTime(today.getTime() + 3600000*24*nDays);

expire.setTime(today.getTime() + (60 * 60 * 1000));

关于javascript - Cookie 一小时后就会过期吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13989474/

相关文章:

javascript - 如何使用 javascript eval 防止意外的全局 namespace 污染?

javascript - if语句计数器变量混淆javascript

javascript - 如果对象在数组中,我如何找到键的总数

javascript - 在首页加载之前设置时区 cookie

javascript - 在 Liferay 中添加非 http only cookie

javascript - 将设置超时函数包装在简单的幻灯片动画 jquery 中

javascript - 如何使用 JavaScript/Jquery 在浏览器关闭选项卡上显示带有自定义 HTML 的弹出窗口?我有自定义 HTML 弹出窗口

php - PHP 中的 AWS Cloudfront SetCookie

javascript - 如果有 cookie,则隐藏代码

ruby-on-rails - 跨域 iframe 导致 cookie 出现问题(Rails)