SetTimeout is triggered immediately if the delay is more than 2147483648 milliseconds

Problem

If the delay greater than 2147483648 milliseconds (24.8551 days), the function is immediately triggered.

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

+7
source share
3 answers

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

This is due to the fact that setTimeout uses a 32-bit integer to store the delay value, so everything above will cause a problem

If you need a timeout that fires after X days a day, you can try using 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); } 

Then you would use this as

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

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

 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 is not too complicated and should expand to the limit of the javascript number, which is 1.7976931348623157E + 10308, which we will all be dead by this number of milliseconds and gone.

To make it possible for you to setLongTimeout, you could modify the function to accept an object that is passed by reference, and thus save the area 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 a timeout and then cancel it later if you need this:

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

Just found this thread, from yesterday I have been experiencing strange behavior on my site. My entire injection unit timer has stopped working or working accidentally. I have a power meter and I get data through mqtt, so I created a stream to save the daily, weekly and monthly data log. First, I save the variable data in the stream, for example, 23:58, then I call this stream and put it in my toolbar and save it in a CSV file. The Azz of these timers was controlled by the injection unit. Is there any way around this? and just use the function node. I use the java script function to get the first day of the month for monthly savings. How to January 1 January 00:03 save the file. So it would be great if I could get rid of all the input nodes and just use the function.

I am a little hopeless in Java, and all my knowledge is gathered in the forums. Never studied this or programming at all. Thank you in advance.

0
source

Source: https://habr.com/ru/post/944025/


All Articles