函数防抖

1.新建comm.js

**
 * 函数防抖
 */
export function debounce(fn, delay) {
  // 记录上一次的延时器
  var timer = null;
  var delay = delay || 200;
  return function() {
    var args = arguments;
    var that = this;
    // 清除上一次延时器
    clearTimeout(timer)
    timer = setTimeout(function() {
        fn.apply(that,args)
    }, delay);
  }
}

2.在vue组件中引入

import {debounce} from '@/utils/comm.js'

3.在组件中使用

<div class="white-search-bar">
        <div class="search-bar-item">
          <input  @on-change="appSearch">
</div>
</div>

methods:{
  appSearch:debounce(function(){
  
this.getAppList()
   },
300)
 }
原文地址:https://www.cnblogs.com/yn-cn/p/13083884.html