Skip to content

Commit

Permalink
Avoided repetative calling of customhook by using a global variable - c…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hariharan committed Oct 2, 2021
1 parent 66baa00 commit 1a3c421
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/hooks/useInterval/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ export const useInterval = (callback: () => void, delay: number | null) => {
return
}

const id = setInterval(() => savedCallback.current(), delay)
// Don't schedule if it is already scheduled
if((window as any).IS_TITLE_CHANGE_RUNNING) {
console.warn('An animation for your document title/fav icon is already running');
return;
}

const id = setInterval(() => savedCallback.current(), delay);
(window as any).IS_TITLE_CHANGE_RUNNING = true;

return () => clearInterval(id)
return () => {
clearInterval(id);
(window as any).IS_TITLE_CHANGE_RUNNING = false;
}
}, [delay])
}
}

0 comments on commit 1a3c421

Please sign in to comment.