diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2012-07-01 22:58:22 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2012-07-05 23:07:27 +0200 |
commit | 0c47219a72fcf58c639ccb37ddf6b01b2261e793 (patch) | |
tree | f232a74b28220e518ab7d2923b066eb7f5f60e77 /lib/timers.js | |
parent | 9126dd2d903f3cf842f0c11a8349d794dde68e98 (diff) | |
download | nodejs-0c47219a72fcf58c639ccb37ddf6b01b2261e793.tar.gz nodejs-0c47219a72fcf58c639ccb37ddf6b01b2261e793.tar.bz2 nodejs-0c47219a72fcf58c639ccb37ddf6b01b2261e793.zip |
timers: fix handling of large timeouts
Don't use the double-negate trick to coalesce the timeout argument into a
number, it produces the wrong result for very large timeouts.
Example:
setTimeout(cb, 1e10); // doesn't work, ~~1e10 == 1410065408
Diffstat (limited to 'lib/timers.js')
-rw-r--r-- | lib/timers.js | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/lib/timers.js b/lib/timers.js index 9e2336d05..6d6066a01 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -170,8 +170,9 @@ exports.active = function(item) { exports.setTimeout = function(callback, after) { var timer; - after = ~~after; - if (after < 1 || after > TIMEOUT_MAX) { + after *= 1; // coalesce to number or NaN + + if (!(after >= 1 && after <= TIMEOUT_MAX)) { after = 1; // schedule on next tick, follows browser behaviour } @@ -222,8 +223,9 @@ exports.setInterval = function(callback, repeat) { if (process.domain) timer.domain = process.domain; - repeat = ~~repeat; - if (repeat < 1 || repeat > TIMEOUT_MAX) { + repeat *= 1; // coalesce to number or NaN + + if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) { repeat = 1; // schedule on next tick, follows browser behaviour } |