使Cookie在一小时内到期 [英] Make a cookie expire in one hour

查看:104
本文介绍了使Cookie在一小时内到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function createCookie()名称,值,天){
if(days){
var date = new Date();
date.setTime(date.getTime()+(days * 60 * 1000));
var expires =; expires =+ date.toGMTString();
}
else var expires =;
document.cookie = name +=+ value + expires +; path = /;
}

函数readCookie(name){
var nameEQ = name +=;
var ca = document.cookie.split(';');
for(var i = 0; i var c = ca [i];
while(c.charAt(0)=='')c = c.substring(1,c.length);
if(c.indexOf(nameEQ)== 0)return c.substring(nameEQ.length,c.length);
}
return null;
}

sWhere =http://google.com;

if(!readCookie('viewcheck')){
createCookie('viewcheck',1,1);
} else {
window.location = sWhere;
}

如何让Cookie在一小时内过期?

解决方案

您的函数使用的是一个 ,而不是从 60 * 1000 开始的60秒(以毫秒为单位)。
你应该将该参数重命名为 minutes ,然后你可以很容易地看到你只需要乘以60的小时数。


I have some code for a cookie that i want to make expire in 1 hour the code is:

    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }

    sWhere = "http://google.com";

    if(!readCookie('viewcheck')){
    createCookie('viewcheck',1,1);
    } else {
    window.location = sWhere;
    }

How can i make the cookie expire in an hour?

解决方案

Your function is using a days argument that is only changing minutes, not days since 60*1000 is 60 seconds in milliseconds. You should rename that argument to minutes, and then you can easily see that you just need to multiply the numberof hours by 60.

这篇关于使Cookie在一小时内到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆