iview Button按钮 全局click事件vue拦截 节流 - 防抖 Throttle debounce

这里是按钮的节流,就没用防抖

const setVueClickGlobalThrottle = Vue => {
  // 节流
  const on = Vue.prototype.$on
  Vue.prototype.$on = function (event, func) {
    console.info('全局拦截 click $on事件 event', event)
    let previous = 0
    let newFunc = func
    if (event === 'click') {
      console.info('全局拦截 click 事件 setVueClickGlobalThrottle')
      newFunc = function () {
        const now = new Date().getTime()
        if (previous + 1000 <= now) {
          func.apply(this, arguments)
          previous = now
        }
      }
    }
    on.call(this, event, newFunc)
  }
}

export default setVueClickGlobalThrottle

main.js 里面加载

import setVueClickGlobalThrottle from '@/libs/common/setVueClickGlobalThrottle.js'

setVueClickGlobalThrottle(Vue) // 将所有click 进行节流处理
posted @ 2022-08-04 16:07  彭成刚  阅读(735)  评论(3编辑  收藏  举报