设置Cookie在2小时后过期 [英] Set a cookie expire after 2 hours

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

问题描述

我有这个JavaScript代码:

I have this JavaScript code:

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

如何让Cookie在2小时后过期?

How can I make the cookie expire after 2 hours?

推荐答案

如果要使用相同类型的函数,请将 days param转换为 hours 并传递2以获得2小时的过期日期。

If your want to use the same type of function , transform the days param into hours and pass 2 to get a 2 hour expiration date.

function spu_createCookie(name, value, hours)
{
    if (hours)
    {
        var date = new Date();
        date.setTime(date.getTime()+(hours*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else
    {
        var expires = "";
    }

    document.cookie = name+"="+value+expires+"; path=/";
}

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

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