setTimeout runs function immediately

Viewed 61

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);
1 Answers

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.

Related