Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 219
Adam Halasz Avatar asked May 01 '13 08:05

Adam Halasz


People also ask

Does setTimeout run immediately?

JavaScript has setTimeout() method which calls a function or evaluates an expression after a specified number of milliseconds.

Is setTimeout in seconds or milliseconds?

Definition and Usage The setTimeout() method calls a function after a number of milliseconds. 1 second = 1000 milliseconds.

Does setTimeout have a limit?

Maximum delay value Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally. This causes an integer overflow when using delays larger than 2,147,483,647 ms (about 24.8 days), resulting in the timeout being executed immediately.

Does setTimeout affect performance?

No significant effect at all, setTimeout runs in an event loop, it doesn't block or harm execution.


2 Answers

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
like image 171
lostsource Avatar answered Oct 26 '22 00:10

lostsource


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);
like image 25
Brian Avatar answered Oct 25 '22 23:10

Brian