Skip to content

Commit

Permalink
fix throttled effect
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Jan 20, 2024
1 parent 0ca7f30 commit ee863ee
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,12 @@ export const useThrottledEffect = (
delay: number,
deps: React.DependencyList
) => {
const lastRan = useRef(Date.now());

useEffect(() => {
const handler = setTimeout(function () {
if (Date.now() - lastRan.current >= delay) {
callback();
lastRan.current = Date.now();
}
}, delay - (Date.now() - lastRan.current));
const handler = setTimeout(() => callback(), delay);

return () => {
clearTimeout(handler);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [delay, ...deps]);
return () => clearTimeout(handler);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [...(deps || []), delay]);
};

export function mean(arr: number[]): number {
Expand Down

0 comments on commit ee863ee

Please sign in to comment.