问题描述
在将大毫秒值传递给 setTimeout()
时,我遇到了一些意外行为。例如,
I came across some unexpected behavior when passing a large millisecond value to setTimeout()
. For instance,
setTimeout(some_callback, Number.MAX_VALUE);
和
setTimeout(some_callback, Infinity);
两者都会导致 some_callback
几乎立即运行,好像我已经通过 0
而不是大数作为延迟。
both cause some_callback
to be run almost immediately, as if I'd passed 0
instead of a large number as the delay.
为什么会发生这种情况?
Why does this happen?
推荐答案
这是由于setTimeout使用32位int来存储延迟所以允许的最大值是
This is due to setTimeout using a 32 bit int to store the delay so the max value allowed would be
2147483647
如果您尝试
2147483648
你得到问题。
我只能假设这导致JS引擎中出现某种形式的内部异常并导致函数触发立刻而不是根本不存在。
I can only presume this is causing some form of internal exception in the JS Engine and causing the function to fire immediately rather than not at all.
这篇关于为什么setTimeout()" break"对于大毫秒延迟值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!