From 1a3c421c55595a7a743b9e6661a242250adc757a Mon Sep 17 00:00:00 2001 From: Hariharan Date: Sun, 3 Oct 2021 00:30:15 +0530 Subject: [PATCH] Avoided repetative calling of customhook by using a global variable - closes #1 --- src/hooks/useInterval/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/hooks/useInterval/index.ts b/src/hooks/useInterval/index.ts index 5ade33d..7be70d7 100644 --- a/src/hooks/useInterval/index.ts +++ b/src/hooks/useInterval/index.ts @@ -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]) -} +}