setTimeout立即运行功能

时间:2020-07-27 02:21:13

标签: javascript momentjs settimeout

我试图在一段时间后使用“ setTimeout”运行“ window.open”功能。如果我使用父项的属性计算时差,然后将输出用于“ setTimeout”,则“ window.open”函数将立即运行。但是,如果我给变量'diffms'一个数字,它就可以正常工作。 我正在使用反应,如何解决此问题?

// const diffms = 10000;
const diffms = moment(this.props.schedule.time).diff(moment());
  setTimeout(() => {
    window.open("https://www.google.com");
}, diffms);

1 个答案:

答案 0 :(得分:1)

SetTimeout具有Maximum Delay Value

Internet Explorer,Chrome,Safari和Firefox等浏览器在内部将延迟存储为32位带符号整数。

当使用大于2147483483647毫秒(约24.8天)的延迟时,这会导致整数溢出,导致超时立即执行。

请在此处查看更多详细信息:

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#:~:text=Maximum%20delay%20value&text=This%20causes%20an%20integer%20overflow,the%20timeout%20being%20executed%20immediately

相关问题