封装
// 防抖
debounce(fn, delay = 100) {
if (typeof fn !== 'function') return
let time = null
return (...args) => {
clearTimeout(time)
time = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
},
使用:1、import引入文件,2、使用函数
import { debounce } from 'xxx'
data.resizeHandler = debounce(() => {
// 具体操作
}, 100)
window.addEventListener('resize', data.resizeHandler)