add throttle decorator

This commit is contained in:
nflnkr 2022-12-24 13:35:58 +03:00
parent ae0050e218
commit 837d6e78b1

29
src/utils/decorators.ts Normal file

@ -0,0 +1,29 @@
export function throttle(func: Function, ms: number) {
let isThrottled = false;
let savedArgs: any;
let savedThis: any;
function wrapper(this: any) {
if (isThrottled) {
savedArgs = arguments;
savedThis = this;
return;
}
func.apply(this , arguments);
isThrottled = true;
setTimeout(function () {
isThrottled = false;
if (savedArgs) {
wrapper.apply(savedThis, savedArgs);
savedArgs = savedThis = null;
}
}, ms);
}
return wrapper;
}