If the delay
is more than 2147483648 milliseconds(24.8551 days) the function will fire immediately.
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
JavaScript has setTimeout() method which calls a function or evaluates an expression after a specified number of milliseconds.
Definition and Usage The setTimeout() method calls a function after a number of milliseconds. 1 second = 1000 milliseconds.
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.
No significant effect at all, setTimeout runs in an event loop, it doesn't block or harm execution.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With