Skip to content

Commit

Permalink
Ensure idle callbacks don't run twice (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton authored Oct 8, 2024
1 parent 65f2e67 commit 19dea67
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lib/whenIdleOrHidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import {runOnce} from './runOnce.js';

/**
* Runs the passed callback during the next idle period, or immediately
* if the browser's visibility state is (or becomes) hidden.
Expand All @@ -26,6 +28,7 @@ export const whenIdleOrHidden = (cb: () => void) => {
if (document.visibilityState === 'hidden') {
cb();
} else {
cb = runOnce(cb);
rIC(cb);
document.addEventListener('visibilitychange', cb, {once: true});
}
Expand Down

0 comments on commit 19dea67

Please sign in to comment.