防抖和节流是什么,区别以及如何实现?

 1. 是什么?

是优化高频率执行代码的一种手段。

比如:浏览器的 resize scroll keypressmousemove 等,在连续被触发时会不断的调用绑定在事件上的回调函数,非常浪费资源和前端性能!

为了优化用户体验,需要对这类事件进行限制调用的次数,我们可以采用 throttle (防抖)和 debounce (节流)的方式减少被触发的频率。

  1.1. 定义

  • 节流:n秒内只运行一次,在n秒内重复被触发,只生效一次
  • 防抖:n秒后执行该事件,在n秒内被重复出发,则重新计时

 简单来说,防抖就是游戏里的回城,点击后在点击会重新计时回城时间,节流就是放技能,一段时间内只能放一次

 2.代码实现

    2.1 节流

思路:每次事件被触发时,如果函数没有在指定的时间间隔内被调用,则调用函数并设置一个计时器,在指定的时间内不在再触发该事件,如果在指定事件内触发事件,则不调用函数,直到间隔时间过去,再重新调用函数

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Document</title>
  <style>
    .box {
      width: 300px;
      height: 300px;
      background-color: #ccc;
      margin: 100px auto;
      text-align: center;
      color: #fff;
      font-size: 50px;
      align-self: center;
    }
  </style>
</head>
<body>
  <div class="box">0</div>
  <script>
    let count = 0
    let flag = true
    document.querySelector('.box').addEventListener('mousemove',function () {
       if(flag){
        this.innerHTML = ++count
        flag = false
      
        setTimeout(function () {
           flag = true
        },500)
       }
    })

  </script>
</body>

</html>

2.2 防抖

思路:每次事件触发时,设置一个定时器,在指定时间内再次被触发,则重新计时,直到时间间隔内有再次触发事件,然后再调用函数

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Document</title>
  <style>
    .box {
      width: 300px;
      height: 300px;
      background-color: #ccc;
      margin: 100px auto;
      text-align: center;
      color: #fff;
      font-size: 50px;
      align-self: center;
    }
  </style>
</head>
<body>
  <div class="box">0</div>
  <script>
      let count = 0 
      let timeId
      document.querySelector('.box').addEventListener('mousemove',function () {
         clearTimeout(timeId)
      
       timeId = setTimeout( () => {
           this.innerHTML = ++count
        },500)
      })
  </script>
</body>
</html>

2.3 lodash实现防抖节流

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Document</title>
  <style>
    .box {
      width: 300px;
      height: 300px;
      background-color: #ccc;
      margin: 100px auto;
      text-align: center;
      color: #fff;
      font-size: 50px;
      align-self: center;
    }
  </style>
</head>

<body>
  <div class="box">0</div>

  <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
  <script>

    let count = 0
    function fn() {
      this.innerHTML = ++count
    }
    // 节流
    document.querySelector('.box').addEventListener('mousemove', _.throttle(fn, 500))

    // 防抖
    document.querySelector('.box').addEventListener('mousemove', _.debounce(fn, 500))

  </script>

</body>

</html>

3.区别

  相同点:

  1.  都是通过 setTimeout 是实现
  2.  都是减少事件触发频率,优化性能

  不同点:

  1.  节流是在一段时间内最多触发一次,节流算法会在特定的间隔时间判断,是否触发事件
  2.  防抖是在一端时间内只要触发事件就会重新计时,直到这段时间没有触发,才触发事件
  3. 节流适用于持续触发,防抖适用于短时间内大量触发
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值