js学习第十五天

防抖

 防抖

 在同一时间内 频繁触发事件,只处理最后一次

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" />
    <script src="./js/lodash.js"></script>
    <script>
      //  性能优化的手段
      //  防抖 - 在同一时间内 频繁触发事件,只处理最后一次
      // 自己实现的防抖函数
      // document.querySelector('input').addEventListener(
      //   'input',
      //   debounce(function () {
      //     console.log('输入')
      //   }, 400)
      // )

      // lodash库
      document.querySelector('input').addEventListener(
        'input',
        _.debounce(function () {
          console.log('输入')
        }, 400)
      )
      /*
        debounce(function () {
          console.log('输入')   
        })
        调用函数  function () {
          console.log('输入')
        } -> fn
        返回值 
        function () {
          setTimeout(function () {
           fn()
          }, 1000)
        }
      */
      // 高阶函数 fn->事件回调
      // function debounce(fn, t) {
      //   let setId
      //   return function () {
      //     clearTimeout(setId)
      //     setId = setTimeout(function () {
      //       fn()
      //     }, t)
      //   }
      // }

      // 思路
      /*
        当事件发生,不立即执行事件回调。给个500毫秒后执行事件回调(定时器),在500毫秒内
        再次触发事件,先取消上次的定时器,再重新开启一个定时器

        // 第一次事件触发 setTimeout(function() { 回调 },500)
        //  300毫秒 事件又触发了一次 clearTimeout取消上一次的定时器,再重开一个定时器
      */
    </script>
  </body>
</html>

节流

 节流

 在同一时间内 频繁触发事件,只执行一次

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>+</button>
    <script src="./js/lodash.js"></script>
    <script>
      //  性能优化的手段
      //  节流 - 在同一时间内 频繁触发事件,只执行一次
      document.querySelector('button').addEventListener(
        'click',
        _.throttle(function () {
          console.log('发请求')
        }, 3000)
      )

      // function throttle(fn, t) {
      //   let flag = false // 一开始 false表示没有任务执行
      //   return function () {
      //     if (flag) return
      //     flag = true
      //     setTimeout(function () {
      //       fn()
      //       flag = false
      //     }, t)
      //   }
      // }

      // 思路
      /*
         当第一次事件发生,把回调函数放到定时器 setTimeout(function () {回调调用},1000) 并且设置开关 把开关状态为true 
         当第二次事件发生  判断开关状态 false 可以处理当前回调, true返回 
      */
    </script>
  </body>
</html>

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值