add throttle decorator
This commit is contained in:
parent
ae0050e218
commit
837d6e78b1
29
src/utils/decorators.ts
Normal file
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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user