You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
658 B
22 lines
658 B
/**
|
|
* 节流修饰器
|
|
* @param delay 节流时间
|
|
*/
|
|
export function throttle(delay: number = 0, rightNow: boolean = false) {
|
|
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
|
|
const func = descriptor.value;
|
|
let timeout: number | null;
|
|
if (typeof func === 'function') {
|
|
// eslint-disable-next-line
|
|
descriptor.value = function (...args: any[]) {
|
|
if (timeout) return;
|
|
const context = this;
|
|
if (rightNow) func.apply(context, args);
|
|
timeout = window.setTimeout(() => {
|
|
func.apply(context, args);
|
|
timeout = null;
|
|
}, delay);
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|