2

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);
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Park
  • 79
  • 5
  • what do you have in your `this.props.scehdule.time`? – critrange Jul 27 '20 at 02:23
  • this.props.schedule.time is now 'Aug 27, 2020 1:24 PM' and if I console log diffms, it is 2743129376. – Park Jul 27 '20 at 02:26
  • 1
    Does this answer your question? [Why does setTimeout() "break" for large millisecond delay values?](https://stackoverflow.com/questions/3468607/why-does-settimeout-break-for-large-millisecond-delay-values) – Patrick Roberts Jul 27 '20 at 02:34

1 Answers1

1

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.

critrange
  • 5,652
  • 2
  • 16
  • 47