Last active
August 10, 2021 17:08
-
-
Save imgsrc/a5765fe83b6f9d78e1476778961d9790 to your computer and use it in GitHub Desktop.
Функция хелпер возвращает вариант func, срабатывающий не чаще чем раз в wait миллисекунд
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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