diff --git a/lib/timers.js b/lib/timers.js index 58a9c98f5d68c9..798ffe7d82dfe8 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -236,7 +236,7 @@ TimerWrap.prototype[kOnTimeout] = function listOnTimeout(now) { // This happens if there are more timers scheduled for later in the list. if (diff < msecs) { var timeRemaining = msecs - (TimerWrap.now() - timer._idleStart); - if (timeRemaining < 0) { + if (timeRemaining <= 0) { timeRemaining = 1; } this.start(timeRemaining); diff --git a/test/sequential/test-timers-set-interval-excludes-callback-duration.js b/test/sequential/test-timers-set-interval-excludes-callback-duration.js index d47659d7b395ab..cb60d7e45270d6 100644 --- a/test/sequential/test-timers-set-interval-excludes-callback-duration.js +++ b/test/sequential/test-timers-set-interval-excludes-callback-duration.js @@ -9,9 +9,16 @@ const t = setInterval(() => { cntr++; if (cntr === 1) { common.busyLoop(100); + // ensure that the event loop passes before the second interval + setImmediate(() => assert.strictEqual(cntr, 1)); first = Timer.now(); } else if (cntr === 2) { assert(Timer.now() - first < 100); clearInterval(t); } }, 100); +const t2 = setInterval(() => { + if (cntr === 2) { + clearInterval(t2); + } +}, 100);