vue 全局防抖处理

261 阅读1分钟

项目需要,实现全局防抖处理,复制下列代码粘贴在main.js中

// 全局防抖处理 -- 最后执行

const on = Vue.prototype.$on
Vue.prototype.$on = function (event, func) {
 let timer
 let newFunc = func
 if (event === 'click') {
   newFunc = function () {
     clearTimeout(timer)
     timer = setTimeout(function () {
       func.apply(this, arguments)
     }, 500)
   }
 }
 on.call(this, event, newFunc)
}