【记录】 vue 输入框使用防抖函数

450 阅读1分钟
//防抖函数 存放在 show.js
function debounce(fn, wait) {
    let timeout = null;
    wait = wait || 600;
    return function () {
      let that = this;
      if(timeout !== null)   clearTimeout(timeout);  
      timeout = setTimeout(() => {
        fn.apply(that);
      }, wait);
    }    
}

export default {
	debounce
}

2.在vue 页面

import Show from '../../common/Show.js';
//输入框
<input type="text" class="" placeholder="搜索标题、编号、姓名或正文内容" v-model="searchVal" @input="inputChange" />

data:{
    searchVal:''
}

//输入框输入方法
inputChange:Show.debounce(function(){
    //自己的业务操作
	console.log(this.searchVal)
}, 1000),