Take the 2-minute tour ×

I came across some unexpected behavior when passing a large millisecond value to setTimeout(). For instance,

setTimeout(some_callback, Number.MAX_VALUE);


setTimeout(some_callback, Infinity);

both cause some_callback to be run almost immediately, as if I'd passed 0 instead of a large number as the delay.

Why does this happen?

share|improve this question
add comment

4 Answers

up vote 25 down vote accepted

This is due to setTimeout using a 32 bit int to store the delay so the max value allowed would be


if you try


you get your problem occurring.

I can only presume this is causing some form of internal exception in the JS Engine and causing the function to fire immediately rather than not at all.

share|improve this answer
Okay, that makes sense. I'm guessing it doesn't actually raise an internal exception. Instead, I see it either (1) causing an integer overflow, or (2) internally coercing the delay to an unsigned 32-bit int value. If (1) is the case, then I'm really passing a negative value for the delay. If it's (2), then something like delay >>> 0 happens, so the delay passed is zero. Either way, the fact that the delay is stored as a 32-bit unsigned int explains this behavior. Thanks! –  Matt Ball Aug 12 '10 at 15:22
Old update, but i've just found the max limit is 49999861776383 (49999861776384 causes the callback to fire instantly) –  maxp Jan 9 at 15:03
add comment

Some explanation here: http://closure-library.googlecode.com/svn/docs/closure_goog_timer_timer.js.source.html

Timeout values too big to fit into a signed 32-bit integer may cause overflow in FF, Safari, and Chrome, resulting in the timeout being scheduled immediately. It makes more sense simply not to schedule these timeouts, since 24.8 days is beyond a reasonable expectation for the browser to stay open.

share|improve this answer
warpech's answer makes a lot of sense - a long running process like a Node.JS server might sound like an exception, but to be honest if you've got something that you want to ensure happens in exactly 24 and a bit days with millisecond accuracy then you should use something more robust in the face of server and machine errors than setTimeout... –  cfogelberg Feb 9 at 12:07
add comment

is actually not an integer. The maximum allowable value for setTimeout is likely 2^31 or 2^32. Try


and you get 1 back instead of 1.7976931348623157e+308.

share|improve this answer
add comment

You can use:

function runAtDate(date, func) {
    var then = date.getTime();
    var diff = Math.max((then - now), 0);
    if (diff > 0x7FFFFFFF) //setTimeout limit is MAX_INT32=(2^31-1)
        setTimeout(function() {runAtDate(date, func);}, 0x7FFFFFFF);
        setTimeout(func, diff);
share|improve this answer
add comment

Your Answer


