Javascript 超时时间会不会太长?

Javascript 超时时间会不会太长?,javascript,Javascript,我正在创建一个应用程序,用于轮询服务器的特定更改。我使用一个使用setTimeout的自调用函数。基本上是这样的: <script type="text/javascript"> someFunction(); function someFunction() { $.getScript('/some_script'); setTimeout(someFunction, 100000); } </script> someFunction(); 函数someFu

我正在创建一个应用程序,用于轮询服务器的特定更改。我使用一个使用setTimeout的自调用函数。基本上是这样的:

<script type="text/javascript">
someFunction();

function someFunction() {
  $.getScript('/some_script');
  setTimeout(someFunction, 100000);
}
</script>

someFunction();
函数someFunction(){
$.getScript('/some_script');
setTimeout(someFunction,100000);
}
为了减少服务器上的轮询强度,我希望有更长的超时间隔;可能在1分钟到2分钟的范围内。是否存在setTimeout的超时时间过长且不再正常工作的点?

setTimeout()
使用32位整数作为其延迟参数。因此,最大值为:

2147483647
与使用递归的setTimeout()相比,我建议使用:


从技术上讲,你还可以。您最多可以有24.8611天的超时时间如果你真的想。setTimeout最长可达2147483647毫秒(32位整数的最大值,约为24天),但如果它高于此值,您将看到意外行为。看

对于间隔,如轮询,我建议使用setInterval而不是递归setTimeout。setInterval完全按照您的要求进行轮询,您也可以进行更多的控制。示例:要随时停止间隔,请确保存储了setInterval的返回值,如下所示:

var guid = setInterval(function(){console.log("running");},1000) ;
//Your console will output "running" every second after above command!

clearInterval(guid) 
//calling the above will stop the interval; no more console.logs!

我不认为它会接近您输入的数字。可能重复:正如Richard指出的,如果您有非常大的超时(大于~24天),您的问题有一个完美的答案:我不同意setInterval而不是recursive setTimeout。setTimeout应该在if中,以防止在需要停止时再次调用它。我不同意递归setTimeout上的setInterval。setTimeout应该在if中,以防止在需要停止时再次调用它。使用recursive setTimeout,您可以确保将它放在函数的末尾,以便仅在函数的其余部分已经完成时调用它。setInterval将调用每个interval,您无法确保最后一次调用已完成。在某些情况下这很重要,在另一些情况下我们不在乎,但我认为意识到这一点是很好的。
var guid = setInterval(function(){console.log("running");},1000) ;
//Your console will output "running" every second after above command!

clearInterval(guid) 
//calling the above will stop the interval; no more console.logs!