/***
* 防抖 只触发最后一次
* @param {Function} fn - 执行事件(函数)
* @param {?String|"click"} event - 事件类型 例:"click","input"
* @param {?Number|300} time - 间隔时间
*
* 例:<el-button v-debounce="[reset,`click`,300]">刷新</el-button>
* 也可简写成:<el-button v-debounce="[reset]">刷新</el-button>
* 例:<el-input v-debounce="[reset,`input`,300]"></el-button>
*/
module.exports={
inserted: function (el, binding) {
let [fn, event = "click", time = 300] = binding.value
let timer
let flag = true;//输入法标记
if (event === "input") {
el.addEventListener("compositionstart", () => {
flag = false;
})
el.addEventListener("compositionend", () => {
flag = true;
})
}
el.addEventListener(event, () => {
timer && clearTimeout(timer)
timer = setTimeout(() => {
if(flag)fn();
}, time)
})
}
}
代码如上所示,可以通过方便的方法对其vue的v-on事件添加防抖截流