1 min read

JavaScript setTimeout 居然立即执行了

在我的经历里,有过类似以下的一段代码:

setTimeout(function() { console.log('sam chen'); }, 2591700000);

Ooooooops,立即执行了。而不是我们所预想的,会在至少 2591700000ms 后触发。

原因是这样的1

Timeout values too big to fit into a signed 32-bit integer may cause overflow in FF, Safari, and Chrome, resulting in the timeout being scheduled immediately. It makes more sense simply not to schedule these timeouts, since 24.8 days is beyond a reasonable expectation for the browser to stay open.

setTimeout 的第二个参数 timeout 的最大值是 21474836472,我们的设定值超过它,回调函数就会立即执行。

报告问题 修订

如果你有自建 https 代理的需求,欢迎尝试 Phantom,一键搭建,方便快捷。查看 demo