Javascript date.setSeconds 在调试器中按预期工作但不在脚本中

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)