setTimeout的大火立刻如果延迟超过2147483648毫秒(setTimeout fires

2019-09-01 14:07发布

问题

如果delay超过2147483648毫秒(24.8551天)函数将立即触发。

setTimeout(function(){ console.log('hey') }, 2147483648) // this fires early
setTimeout(function(){ console.log('hey') }, 2147483647) // this works properly

我试了一下下的Chrome V26和Node.js的v8.21

Answer 1:

的setTimeout的上限是0x7FFFFFFF (或2147483647十进制)

这是因为setTimeout的使用32位整数来存储它的延迟值,所以上面的东西,这将导致问题

如果你想在哪几天的X ammount的后触发超时,你可以尝试使用setInterval使用,而不是像这样较低的延迟值

function setDaysTimeout(callback,days) {
    // 86400 seconds in a day
    var msInDay = 86400*1000; 

    var dayCount = 0;
    var timer = setInterval(function() {
        dayCount++;  // a day has passed

        if(dayCount == days) {
           clearInterval(timer);
           callback.apply(this,[]);
        }
    },msInDay);
}

那么你可以使用它像这样

setDaysTimeout(function() {
     console.log('Four days gone');
},4); // fire after 4 days


Answer 2:

既然你都限制在32位,只是包装的setTimeout在像这样一个递归函数:

function setLongTimeout(callback, timeout_ms)
{

 //if we have to wait more than max time, need to recursively call this function again
 if(timeout_ms > 2147483647)
 {    //now wait until the max wait time passes then call this function again with
      //requested wait - max wait we just did, make sure and pass callback
      setTimeout(function(){ setLongTimeout(callback, (timeout_ms - 2147483647)); },
          2147483647);
 }
 else  //if we are asking to wait less than max, finally just do regular setTimeout and call callback
 {     setTimeout(callback, timeout_ms);     }
}

这是不是太复杂,应该是可扩展多达数的JavaScript是1.7976931348623157E + 10308,其中由数毫秒,我们都将死了的极限。

也让它这样你就可以有能力setLongTimeout,你可以修改函数接受通过引用传递的对象,因此保持范围返回到调用函数:

function setLongTimeout(callback, timeout_ms, timeoutHandleObject)
{
 //if we have to wait more than max time, need to recursively call this function again
 if(timeout_ms > 2147483647)
 {    //now wait until the max wait time passes then call this function again with
      //requested wait - max wait we just did, make sure and pass callback
      timeoutHandleObject.timeoutHandle = setTimeout(function(){ setLongTimeout(callback, (timeout_ms - 2147483647), timeoutHandleObject); },
          2147483647);
 }
 else  //if we are asking to wait less than max, finally just do regular setTimeout and call callback
 {     timeoutHandleObject.timeoutHandle = setTimeout(callback, timeout_ms);     }
}

现在,你可以调用超时,再后来取消它,如果你需要像这样:

var timeoutHandleObject = {};
setLongTimeout(function(){ console.log("Made it!");}, 2147483649, timeoutHandleObject);
setTimeout(function(){ clearTimeout(timeoutHandleObject.timeoutHandle); }, 5000);


文章来源: setTimeout fires immediately if the delay more than 2147483648 milliseconds