Skip to content

Commit e22d23b

Browse files
authored
Remove visibiliytchange event listeners when no longer required (#627)
* Remove visibiliytchange event listeners when no longer required * Update comment * Wording cleanup
1 parent 1509d64 commit e22d23b

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/lib/whenIdleOrHidden.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ export const whenIdleOrHidden = (cb: () => void) => {
2929
cb();
3030
} else {
3131
cb = runOnce(cb);
32-
rIC(cb);
3332
document.addEventListener('visibilitychange', cb, {once: true});
33+
rIC(() => {
34+
cb();
35+
// Remove the above event listener since no longer required.
36+
// See: https://github.com/GoogleChrome/web-vitals/issues/622
37+
document.removeEventListener('visibilitychange', cb);
38+
});
3439
}
3540
};

0 commit comments

Comments
 (0)