简体   繁体   中英

setTimeout runs function immediately

I am trying to use 'setTimeout' to run 'window.open' function after certain period of time. If I calculate the time difference with the parent's property and then use the output for 'setTimeout', the 'window.open' function runs immediately. However, if I just give a number to the variable 'diffms', it works fine. I am using react and how can I fix this problem?

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

SetTimeout has Maximum Delay Value

Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally.

This causes an integer overflow when using delays larger than 2,147,483,647 ms (about 24.8 days), resulting in the timeout being executed immediately.

Please check more details here:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM