防抖函数和节流函数

一、节流throttle

控制事件的触发频率,针对高频触发的事件,在指定时间内只触发一次。--resize

以下用时间戳和定时器的方法实现一个简单的节流函数;

示例是一个盒子中的鼠标移动所触发的事件

html代码(添加了一点点样式):

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

<style>
  body {
    display: flex;
    justify-content: center;
  }

  .box {
    width: 200px;
    height: 200px;
    background-color: rgb(179, 172, 172);
    text-align: center;
    margin: auto;
    line-height: 200px;
    font-size: 24px;
    color: red;
  }
</style>

js代码:

const box = document.querySelector('.box')
  box.addEventListener('mousemove', throttle(move, 500))

  // 1.节流函数
  // 方法1:时间戳
  function throttle(fn, delay) {
    // 节流阀
    let temp = false;
    let time = Date.now()
    if (temp) {
      return
    } else {
      temp = true
      return function () {
        if (Date.now() - time > delay) {
          fn()
          time = Date.now()
        }
      }
    }
  }
  // 方法2:使用定时器
  function throttle(fn, delay) {
    let timer;
    return function () {
      if (!timer) {
        timer = setTimeout(function () {
          timer = null;
          fn()
          temp = false;
        }, delay)
      }
    }
  }

  // 2.业务逻辑
  function move() {
    box.innerHTML = ++box.innerHTML
    console.log('移动');
  }

应用场景(resize):

  function throttle(fn, delay) {
    // 节流阀
    let temp = false;
    return function () {
      if (temp) {
        return;
      } else {
        temp = true;
        setTimeout(() => {
          fn.apply(this, arguments);
          temp = false;
        }, delay);
      }
    };
  }
  function visInterface(e) {
    console.log(e.target.innerWidth, e.target.innerHeight);
  }
  window.addEventListener('resize', throttle(visInterface, 1000));

二、防抖debounce

当事件被触发后,延迟一定时间后触发回调,如果在这期间又被触发,则重新计时;

一般用于高频触发的事件,只触发最后一次--input搜索

应用示例:

// 获取dom元素
  const input = document.querySelector('#input')
  // 给元素添加事件
  input.addEventListener('input', debounce(methodBtn, 500))

  // 防抖函数
  function debounce(fn, delay) {
    let timer
    return function () {
      clearTimeout(timer)
      timer = setTimeout(function () {
        fn()
      }, delay)
    }
  }
  // 业务逻辑
  function methodBtn() {
    console.log(input.value);
  }

三、lodash

当然,如果我们实在不想自己写防抖和节流函数的话,我们可以使用lodash封装好的方法

我们先下载lodash依赖,同时引入

我们用lodash的内置_.debounce()和_.throttle来做我们的防抖函数和节流函数,这个写法跟以上我们自行封装的防抖节流函数逻辑大概相同,都是接受两个参数,第一个是我们业务逻辑的回调,第二个就是延时时间。

<body>
  <input type="text" id="input">
</body>

<script src="./node_modules/lodash/lodash.js"></script>
<script>
  // 获取dom元素
  const input = document.querySelector('#input')
  // 给元素添加事件
  input.addEventListener('input', _.debounce(methodBtn, 500))

 // 业务逻辑
  function methodBtn() {
    console.log(input.value);
  }
</script>
<style>
  body {
    display: flex;
    justify-content: center;
  }

  .box {
    width: 200px;
    height: 200px;
    background-color: rgb(179, 172, 172);
    text-align: center;
    margin: auto;
    line-height: 200px;
    font-size: 24px;
    color: red;
  }
</style>

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

<script src="./node_modules/lodash/lodash.js"></script>
<script>
  const box = document.querySelector('.box')
  box.addEventListener('mousemove', _.throttle(move, 500))

  // 业务逻辑
  function move() {
    box.innerHTML = ++box.innerHTML
    console.log('移动');
  }
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值