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

Javascript date.setSeconds在调试器中按预期工作,但在脚本中不工作,javascript,cookies,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} secon

我有一个设置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=/`;
}
现在,如果我在调试器中对行执行此操作,它将按预期工作:

但当我让脚本运行并登录到控制台时,我得到了3分钟的时间和秒数:

我在这里遗漏了一个奇怪的javascript计时吗?

显然
setSeconds()
指的是当前时间。您可以使用
alert()
查看更大的差异。 但是,以下操作将按照您的预期进行: 替换:

与:

请注意,我是零毫秒,您可以随意处理它。
请参见工作示例使用此代码,但请确保持续时间以毫秒为单位,因此,如果要添加2秒,则需要通过2000秒,或者如果要通过秒,则只需在代码中添加持续时间*1000

函数createCookieWithDuration(名称、值、持续时间){
const date=新日期();
log(`datenow:${date}`);
const newDate=新日期(Date.getTime()+持续时间);
log(`adjusted date by${duration}:${newDate}`);
document.cookie=`${name}=${value};expires=${newDate};path=/`;

}
它在Windows 10上的chrome 71.0.3578.98(官方版本)(64位)上运行正常。我看不到任何额外的秒数增加。
date.setSeconds(date.getSeconds() + duration);
    date.setHours(date.getHours(), date.getMinutes(), date.getSeconds()+ duration, 0);