Skip to content

Commit 12bad69

Browse files
remusaojasnell
authored andcommitted
timers: fix clearInterval to work with timers from setTimeout
According to HTML Living Standard, "either method [clearInterval or clearTimeout] can be used to clear timers created by setTimeout() or setInterval().". The current implementation of clearTimeout is already able to destroy a timer created by setInterval, but not the other way around. PR-URL: #19952 Refs: https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
1 parent aa5ae9e commit 12bad69

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

doc/api/timers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ Returns a reference to the `Immediate`.
5656
## Class: Timeout
5757

5858
This object is created internally and is returned from [`setTimeout()`][] and
59-
[`setInterval()`][]. It can be passed to [`clearTimeout()`][] or
60-
[`clearInterval()`][] (respectively) in order to cancel the scheduled actions.
59+
[`setInterval()`][]. It can be passed to either [`clearTimeout()`][] or
60+
[`clearInterval()`][] in order to cancel the scheduled actions.
6161

6262
By default, when a timer is scheduled using either [`setTimeout()`][] or
6363
[`setInterval()`][], the Node.js event loop will continue running as long as the

lib/timers.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,13 @@ exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
496496
return timeout;
497497
};
498498

499-
exports.clearInterval = function(timer) {
500-
if (timer && timer._repeat) {
501-
timer._repeat = null;
502-
clearTimeout(timer);
503-
}
499+
exports.clearInterval = function clearInterval(timer) {
500+
// clearTimeout and clearInterval can be used to clear timers created from
501+
// both setTimeout and setInterval, as specified by HTML Living Standard:
502+
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
503+
clearTimeout(timer);
504504
};
505505

506-
507506
function unrefdHandle(timer, now) {
508507
try {
509508
// Don't attempt to call the callback if it is not a function.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// This test makes sure that timers created with setTimeout can be disarmed by
5+
// clearInterval and that timers created with setInterval can be disarmed by
6+
// clearTimeout.
7+
//
8+
// This behavior is documented in the HTML Living Standard:
9+
//
10+
// * Refs: https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
11+
12+
// Disarm interval with clearTimeout.
13+
const interval = setInterval(common.mustNotCall(), 1);
14+
clearTimeout(interval);
15+
16+
// Disarm timeout with clearInterval.
17+
const timeout = setTimeout(common.mustNotCall(), 1);
18+
clearInterval(timeout);

0 commit comments

Comments
 (0)