简体   繁体   English

JavaScript setTimeout 无法正常工作

[英]JavaScript setTimeout doesn't work properly

I read recently that setTimeout() 's timeout limit is 2147483647 .我最近读到setTimeout()的超时限制是2147483647

When I set the timeout to 2147483648 the program changes it to 1 but doesn't do when the timeout is set to 52147483648 .当我将超时设置为2147483648时,程序将其更改为1但在超时设置为52147483648时不会这样做。

 setTimeout(() => console.log(2147483648), 2147483648) // will fire in 1ms setTimeout(() => console.log(52147483648), 52147483648) // will fire in a long time

What is happening here?这里发生了什么?

Maximum delay value Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally.最大延迟值 包括 Internet Explorer、Chrome、Safari 和 Firefox 在内的浏览器在内部将延迟存储为 32 位签名的 integer。 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.当使用大于 2,147,483,647 毫秒(约 24.8 天)的延迟时,这会导致 integer 溢出,从而导致立即执行超时。

settimeout 设置超时

As per the Documentation, the setTimeOut Function supports a maximum of 24.8 days [MDN Ref] that is equal to 2147483647根据文档,setTimeOut Function 最多支持 24.8 天[MDN Ref]等于 2147483647

Thus when you do 2147483648 you essentially get is (2147483648 - 2147483647 = 1)因此,当您执行 2147483648 时,您基本上得到的是 (2147483648 - 2147483647 = 1)

However, when you use 52147483648 as per the epoch time converter this is only 17 days ahead in the future which is less than 24.8 days thus it does not convert it但是,当您根据纪元时间转换器使用 52147483648 时,这仅提前 17 天,不到 24.8 天,因此它不会转换它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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