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