0

我捏住了下面的 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
     }
});
4

2 回答 2

2

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

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

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

于 2012-12-21T11:40:05.160 回答
1

我会使用 getHours 方法。

var expire = new Date();
expire.setHours(expire.getHours()+1);
//test
alert(expire.toUTCString());
于 2012-12-21T14:37:18.630 回答