【问题标题】:Set jQuery Modal Cookie to Expire in 7 days将 jQuery Modal Cookie 设置为 7 天后过期
【发布时间】:2021-11-02 05:09:36
【问题描述】:

我需要让我的 jquery 模态仅出现在页面加载时出现在主页上,以便在 7 天后重新出现(onload)。这是我当前的代码,我对日期持续时间感到困惑。我不知道要改变什么。

这是我当前的代码:

$(window).on('load',function(){
if (document.cookie.indexOf('visited=true') == -1){
   
$('#ex1').modal({show:true});
var year = 1000*60*60*24*7;
var expires = new Date((new Date()).valueOf() + year);
document.cookie = "visited=true;expires=" + expires.toUTCString();
 }
});

【问题讨论】:

  • 使用浏览器的控制台,过期计算显示:Date Fri Sep 10 2021 23:08:18 GMT-0400 (Eastern Daylight Time)toUTCSting()显示:"Sat, 11 Sep 2021 03:08:21 GMT"
  • 有没有更简单的方法来设置 7 天?

标签: javascript jquery cookies onload


【解决方案1】:

尝试使用此功能,可重复使用且易于阅读

function setCookie(variable, value, expires_days) { 
    let d = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * expires_days);
    document.cookie = variable + '=' + value + '; expires=' + d.toGMTString() + ';';
}
setCookie("visited", "true", 7);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2011-08-30
    相关资源
    最近更新 更多