【问题标题】:Custom cookie lasts 5 seconds自定义 cookie 持续 5 秒
【发布时间】:2020-12-16 20:58:56
【问题描述】:

我正在尝试设置一个 cookie 以仅显示一次模式。我试过像这样使用 jQuery cookie:

$.cookie("cookie", "0", {expires: date});

其中日期已被尝试为整数和当前日期 + 整数的总和:

var date = new Date();
date.setDate(date.getDate() + 15);

我还尝试在我的 gulpconfig.js 中的 src/js 文件中添加 js.cookie,并使用“花式”代码:

Cookies.set('cookiename','0',{expires: date}

但结果相同。我关闭模式,如果我立即刷新(前 5 秒),console.log() 将显示其值已正确更改为 1,如果我继续刷新,它会继续重置其过期 5 秒,但如果我等待这秒再刷新,值又为0,模态回来了。

【问题讨论】:

  • 如果你在这个用例的页面中只包含$cookie,使用localStorage会更简单

标签: javascript jquery cookies drupal drupal-8


【解决方案1】:
let date = new Date();
date = date.getDate(); //extracting cuurent date from date object
date = date + 15 ; //adding 15 more days to it.

$.cookie("cookie", 0, { expires : date }); //cookie expires after 15 days.

删除 cookie

$.removeCookie("test");

【讨论】:

    【解决方案2】:

    原来我必须在 EU Cookie 合规模块中将 cookie 列入白名单...不知道它会默认禁用功能性 cookie。

    【讨论】:

      猜你喜欢
      • 2021-07-13
      • 1970-01-01
      • 2018-07-15
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 2011-08-12
      • 2016-02-15
      相关资源
      最近更新 更多