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