使用泛型定义
function debounce<ThisType>(
func: (this: ThisType, ...args: any[]) => void,
delay = 100
) {
let timer: number;
return function (this: ThisType, ...args: any[]) {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}