Cookie会在一小时后过期吗?

时间:2012-12-21 11:38:38

标签: 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
     }
});

2 个答案:

答案 0 :(得分:2)

Javascript时间以毫秒表示,以便更改

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

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

答案 1 :(得分:1)

我会使用getHours方法。

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