Skip to content

Commit

Permalink
prevent doubling up on visibilitychange event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan4m1 committed Feb 7, 2024
1 parent 9d0cca5 commit 044d9ad
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/hooks/useTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export default function useTimer(run = true, interval = 1000) {
}, [running, incrementTimer, interval]);

useEffect(() => {
// if we remove visibilitychange on unmount we lose it, so only set it up once
// if we remove visibilitychange on unmount we lose it, so always remove it
// before adding it to ensure it only exists once
document.removeEventListener(
'visibilitychange',
handleDocumentVisibilityChange
);
document.addEventListener(
'visibilitychange',
handleDocumentVisibilityChange
Expand Down

0 comments on commit 044d9ad

Please sign in to comment.