Skip to content

Instantly share code, notes, and snippets.

@imgsrc
Last active August 10, 2021 17:08
Show Gist options
  • Save imgsrc/a5765fe83b6f9d78e1476778961d9790 to your computer and use it in GitHub Desktop.
Save imgsrc/a5765fe83b6f9d78e1476778961d9790 to your computer and use it in GitHub Desktop.
Функция хелпер возвращает вариант func, срабатывающий не чаще чем раз в wait миллисекунд
export const debounce = (func: AnyFunction, wait: number) => {
let timer: number | null = null;
return function (this: unknown, ...args: unknown[]) {
const onComplete = () => {
func.apply(this, args);
timer = null;
};
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(onComplete, wait);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment