front-standard-utils
Version:
17 lines (16 loc) • 459 B
text/typescript
/***
* 防抖函数
* @param fn 事件触发的操作
* @param delay 可选,多少毫秒内连续触发事件延迟执行,默认200毫秒
* @returns {Function}
*/
export const debounce = (fn: Function, delay = 200) => {
let timer: NodeJS.Timeout | null
return function (this: any) {
let this_ = this, args: any = arguments
timer && clearTimeout(timer)
timer = setTimeout(function () {
fn.apply(this_, args)
}, delay)
}
}