饼干在一个小时后到期



我捏了以下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时间在millseconds中表示,因此更改

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

to

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

我会使用gethours方法。

var expire = new Date();
expire.setHours(expire.getHours()+1);
//test
alert(expire.toUTCString());

最新更新