vue: 输入防抖实现

1、自定义防抖函数

export function debounce(func, delay=600) {
  let timer

  return function (...args) {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      func.apply(this, args)
    }, delay)
  }
}

2、输入框

<input v-model.trim="searchKey" onInput="onSearchInput"/>

3、防抖函数调用

onSearchInput: debounce(function(e){
    let self = this;
    self.searchKey = e.mp.detail.value;
    ...your code
},600)

 

posted @ 2021-05-10 16:15  Nyan  阅读(793)  评论(0编辑  收藏  举报