javascript - Angularjs 设置 Cookie 在 20 分钟内过期

标签 javascript angularjs cookies

我想设置将在 20 分钟后过期的 cookie。我可以设置一天设置。我该如何应对这种情况?

var date = new Date();
cookies.put("userName","My Name",{'expires':date.getDate()+1});
cookies.put("userNickName","My NickName",{'expires':date.getDate()+1});
cookies.put("userID","My ",{'expires':date.getDate()+1});

最佳答案

我已经有了答案哈哈。我必须使用@Suren Srapyan 提供的代码设置“日期”变量以增加 20 分钟,然后将其传递给 expires 属性。所以它会像这样

//declare date and get current date time
var date = new Date();
//add 20 minutes to date
date.setTime(date.getTime() + (20* 60 * 1000));

//pass the "date" variable to expires properity
cookies.put("userName","My Name",{'expires':date});
cookies.put("userNickName","My NickName",{'expires':date});
cookies.put("userID","My ",{'expires':date});

关于javascript - Angularjs 设置 Cookie 在 20 分钟内过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42134432/

相关文章:

javascript - 为什么 !new Boolean(false) 在 JavaScript 中等于 false?

javascript - 我可以在 Javascript 中打开一个新窗口并单击该窗口中的一个元素吗?

javascript - Angular Material - 如何将自定义指令属性附加到 md-list-item 元素?

javascript - 在 AngularJS 表达式中使用变量?

php - 使用 cookie 在 PHP 中创建 "remember me"系统的安全方法是什么?

google-chrome - Set-Cookie 在 Chrome 中不起作用

javascript - 使用 ng-repeat 和 foundation accordion 的动画

javascript - JavaScript 循环中的 .length -1 与 .length

angularjs - 优雅地处理 AngularJS 错误 : $injector:nomod Module Unavailable

java - JAX-RS Cookie 响应未在浏览器中显示