Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bnoordhuis committed Jul 5, 2012
1 parent 9126dd2 commit 0c47219
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/timers.js
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion test/simple/test-timers.js
Expand Up @@ -45,7 +45,8 @@ var inputs = [
1,
1.0,
10,
2147483648 // browser behaviour: timeouts > 2^31-1 run on next tick
2147483648, // browser behaviour: timeouts > 2^31-1 run on next tick
12345678901234 // ditto
];

var timeouts = [];
Expand Down

0 comments on commit 0c47219

Please sign in to comment.