每小时设置javascript cookie到期

时间:2015-02-08 09:01:19

标签: javascript cookies

我在下面有这个代码。我需要将cookie更改为每小时过期,但将1设置为此行并不会有帮助(tomorrow.setHours(6,0,0,0)

  // cookie
                var now  = new Date();
                var next = new Date(now.setTime(now.getTime() + sWait));
                document.cookie = cookie + '=1;expires=' + next.toGMTString() + ';path=/';
                var tomorrow = new Date(); tomorrow.setHours(6,0,0,0);
                document.cookie = cookie + 'Cap=' + (popsToday + 1) + ';expires=' + tomorrow.toGMTString() + ';path=/';
                pop2under();
            }
        };

3 个答案:

答案 0 :(得分:0)

您可以在How to set a cookie to expire in 1 hour in Javascript?

找到解决方案

解决方案是

var now = new Date();
var time = now.getTime();
time += 3600 * 1000 * 3;
now.setTime(time);
document.cookie = 
'username=' + value + 
'; expires=' + now.toUTCString() + 
'; path=/';

答案 1 :(得分:0)

您只需将该行更改为以下内容:

tomorrow.setHours(tomorrow.getHours()+1);

答案 2 :(得分:0)

试试这个

if (document.cookie.indexOf('visited=true') == -1) {
    var onehour = 1000 * 60 * 60 * 1 * 1;
    var expires = new Date((new Date()).valueOf() + onehour);
    document.cookie = "visited=true;expires=" + expires.toUTCString();
    // your code
}