Skip to content

Instantly share code, notes, and snippets.

@ztrehagem
Created September 14, 2023 01:21
Show Gist options
  • Save ztrehagem/db2ebce9101ce3fe52d9a2ab9c68c3f0 to your computer and use it in GitHub Desktop.
Save ztrehagem/db2ebce9101ce3fe52d9a2ab9c68c3f0 to your computer and use it in GitHub Desktop.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const debounce = <T extends (...args: any[]) => void>(
fn: T,
timeout: number,
): T => {
let timeoutId: number | undefined;
return new Proxy(fn, {
apply: (target, thisArg, args) => {
window.clearInterval(timeoutId);
timeoutId = window.setTimeout(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
target.call(thisArg, ...args);
}, timeout);
},
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment