ror*_*ory 5 javascript cookies
我有一个设置 cookie 的函数,如下所示:
function createCookieWithDuration(name, value, duration) {
const date = new Date();
console.log(`date now: ${date}`);
date.setSeconds(date.getSeconds() + duration);
console.log(`adjusted date by ${duration} seconds: ${date}`);
document.cookie = `${name}=${value}; expires=${date}; path=/`;
}
Run Code Online (Sandbox Code Playgroud)
但是当我让脚本运行并登录到控制台时,我会添加 3 分钟以及秒数:
有没有我在这里遗漏的奇怪的 javascript 计时问题?
小智 1
使用此代码,但请确保持续时间以毫秒为单位,因此如果要添加 2 秒,则需要传递 2000,或者如果要传递秒,则只需在代码中添加持续时间 * 1000。
function createCookieWithDuration(name, value, duration) {
const date = new Date();
console.log(`date now: ${date}`);
const newDate = new Date(date.getTime() + duration);
console.log(`adjusted date by ${duration}: ${newDate}`);
document.cookie = `${name}=${value}; expires=${newDate}; path=/`;
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
523 次 |
| 最近记录: |