vue防抖和节流

7 篇文章 0 订阅
// 获取联想菜单
    getSearchSuggFn () {
    //>>>>>>>防抖
      // 1. 关闭 上一次 延迟器
      if (this.timerId) {
        clearTimeout(this.timerId)
      }
      // 2. 重新开启一次延迟器,如果在 500 毫秒之内又
      // 再次 输入了,那么这个延迟器 就会 被 关闭
      this.timerId = setTimeout(async () => {
        if (!this.keyword) return
        // 调用 联想菜单 接口 - 获取数据
        const resp = await getSearchSuggListAPI({
          keyword: this.keyword
        })
     //>>>>>>>>
        // 数据 预处理
        // 数据 预处理思想:后端返回的数据 在赋值 给 vue数据变量之前,
        // 在赋值之前的数据处理称之为数据预处理
     //>>>>>>>>>>>字符串去空格
        const { options } = resp.data
        // 1. for循环版本
        // for (let i = 0; i < options.length; i++) {
        //   options[i] = options[i].replace(/ /g, '')
        // }
        // 2. forEach版本
        // options.forEach((str, idx) => {
        //   options[idx] = str.replace(/ /g, '')
        // })
        // 3. map版本
        this.suggList = options.map(str => str.replace(/ /g, ''))
      }, 500)
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值