/**
* 防抖指令 单位时间只触发最后一次
* @param {Function} fn - 执行事件
* @param {?String|"click"} event - 事件类型 例:"click"
* @param {?Number|500} time - 间隔时间
* @param {Array} binding.value - [fn,event,time]
* 直接使用: v-debounce="fn"
* 配置事件,间隔时间: v-debounce="[fn,'click',500]"
* 事件传递参数则: v-debounce="[()=>fn(param),`click`,500]"
*/
export default {
bind(el, binding) {
try {
let fn, event = "click",
time = 500;
if (typeof binding.value == 'function') {
fn = binding.value
} else {
[fn, event = "click", time = 500] = binding.value
}
let timer;
el.addEventListener(event, () => {
timer && clearTimeout(timer)
timer = setTimeout(() => fn(), time)
})
} catch (e) {
console.log(e)
}
}
}
标签:防抖,binding,param,event,指令,500,click,fn From: https://www.cnblogs.com/Privatexaio/p/17697441.html