setTimeout fires immediately if the delay more than 2147483648 milliseconds

こ雲淡風輕ζ 提交于 2019-12-09 16:18:15

问题


The problem

If the delay is more than 2147483648 milliseconds(24.8551 days) the function will fire immediately.

Example

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

I tried it under Chrome v26 and Node.js v8.21


回答1:


The upper limit of setTimeout is 0x7FFFFFFF (or 2147483647 in decimal)

This is because setTimeout uses a 32bit integer to store its delay value, so anything above that will cause the problem

If you want a timeout which fires after an X ammount of days, you could try to use setInterval instead with a lower delay value like this

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);
}

You would then use it like this

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



回答2:


Since you are limited to 32 bits, just wrap setTimeout in a recursive function like so:

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);     }
}

This isn't too complicated and should be extensible up to the limit of javascript number which is 1.7976931348623157E+10308, which by that number of milliseconds, we will all be dead and gone.

Too make it so you can have the ability to setLongTimeout, you could modify the function to accept an object which is passed by reference and thus retain scope back to the calling function:

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);     }
}

Now you can call the timeout and then cancel it later if you needed like so:

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


来源:https://stackoverflow.com/questions/16314750/settimeout-fires-immediately-if-the-delay-more-than-2147483648-milliseconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!