最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout fires immediately if the delay more than 2147483648 milliseconds - Stack Overflow

programmeradmin7浏览0评论

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

Share Improve this question asked May 1, 2013 at 8:09 Adam HalaszAdam Halasz 58.3k67 gold badges153 silver badges216 bronze badges 2
  • v8 must store the delay as a 32 bit signed integer. If you overflow, you'll get a negative number. Which logically, would execute right away. – Daniel Commented May 1, 2013 at 8:15
  • possible duplicate of Why does setTimeout() "break" for large millisecond delay values? – Oriol Commented Jan 28, 2014 at 23:35
Add a comment  | 

2 Answers 2

Reset to default 13

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);
发布评论

评论列表(0)

  1. 暂无评论