vue中使用防抖节流函数

485 阅读1分钟
1.在utils文件夹下新建common.js文件
/**
 * 函数防抖
 */
export function debounce (fn, delay) {
  // 记录上一次的延时器
  var timer = null
  var delay1 = delay || 200
  return function () {
    var args = arguments
    var that = this
    // 清除上一次延时器
    clearTimeout(timer)
    timer = setTimeout(function () {
      fn.apply(that, args)
    }, delay1)
  }
}
引入这个防抖组件
  • 这里没有使用箭头函数的原因是 会有this指向的问题 所以使用箭头函数
// 引入防抖组件
import { debounce } from '../utils/common'
用这个防抖
    // 实时搜索
    onInput: debounce(async function () {
      const res = await GetTimeData({
        keyword: this.inputVal
      })
      if (res.errno === 0) {
        this.listArr = res.data
      }
    }, 200)
    ```---
# 主题列表:juejin, github, smartblue, cyanosis, channing-cyan, fancy, hydrogen, condensed-night-purple, greenwillow, v-green, vue-pro, healer-readable, mk-cute, jzman, geek-black, awesome-green, qklhk-chocolate
# 贡献主题:https://github.com/xitu/juejin-markdown-themes
theme: juejin
highlight:
---