美文网首页
vue防抖节流指令

vue防抖节流指令

作者: w_小伍 | 来源:发表于2020-06-11 17:47 被阅读0次

参考:https://juejin.im/post/5e328dd85188254e1b0c6e5b

在src下新建directives文件夹,新建index.js

import Vue from 'vue'
/***
 * 防抖 单位时间只触发最后一次
 *  @param {?Number|300} time - 间隔时间
 *  @param {Function} fn - 执行事件
 *  @param {?String|"click"} event - 事件类型 例:"click"
 *  @param {Array} binding.value - [fn,event,time]
 *  例:<el-button v-debounce="[reset,`click`,300]">刷新</el-button>
 *  也可简写成:<el-button v-debounce="[reset]">刷新</el-button>
 */
Vue.directive('debounce', {
  inserted: function (el, binding) {
    let [fn, event = "click", time = 300] = binding.value
    let timer
    el.addEventListener(event, () => {
      timer && clearTimeout(timer)
      timer = setTimeout(() => fn(), time)
    })
  }
})

/***
 *  节流 每单位时间可触发一次
 *  第一次瞬间触发,最后一次不管是否达到间隔时间依然触发
 * 【考虑到input的change事件】
 *  @param {?Number|300} time - 间隔时间
 *  @param {Function} fn - 执行事件
 *  @param {?String|"click"} event - 事件类型 例:"click"
 *  @param {Array} binding.value - [fn,event,time]
 *  例:<el-button v-throttle="[reset,`click`,300]">刷新</el-button>
 *  传递参数则:<el-button v-throttle="[()=>reset(param),`click`,300]">刷新</el-button>
 */
Vue.directive('throttle', {
  inserted: function (el, binding) {
    let [fn, event = "click", time = 300] = binding.value
    let timer, timer_end;
    el.addEventListener(event, () => {
      if (timer) {
        clearTimeout(timer_end);
        return timer_end = setTimeout(() => fn(), time);
      }
      fn();
      timer = setTimeout(() => timer = null, time)
    })
  }
})

使用
在main.js里引入

import './directives/index'

页面使用

<input type="text" v-model="messageInputV" v-debounce="[searchV,`keyup`,2000]">
searchV() {
      console.log(this.messageInputV)
    },

相关文章

网友评论

      本文标题:vue防抖节流指令

      本文链接:https://www.haomeiwen.com/subject/jzxitktx.html