简体   繁体   中英

setTimeout fires immediately if the delay more than 2147483648 milliseconds

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM