JavaScript基础 -- 防抖和节流

防抖和节流

  • 什么时候用到防抖和节流

    • 当某一个事件的触发频率非常高,如,用户点击按钮、用户移动鼠标,为了节省浏览器资源,就需要对事件的执行做出限制。
  • 防抖

    • 防抖,就是在用户点击按钮、用户移动鼠标触发事件后,需要等待一段时间再执行事件函数,如果在等待的过程中,这个事件再次触发,那么重新计时等待。

      <!DOCTYPE html>
      <html>
          <head>
              <meta charset = "utf-8"/>
              <link type = "text/css" rel = "stylesheet" href = "a.css"/>
              <style type = "text/css"></style>
          </head>
          <body>
              <h1>防抖</h1>
              <input id = "btn" type = "button" value = "add"/>
              <h2>数字</h2>
              <span id = "num">0</span>
          </body>
          <script>
              let button = document.getElementById('btn');
              let number = document.getElementById('num');
      
              let m = 0;
              let time;
              let isOk = true;
      
              //不使用闭包,思路清晰。
              button.onclick = function()
              {
                  console.log("xxxxxx");
      
                  clearTimeout(time);
      
                  time = setTimeout(() => {
                      number.innerHTML = ++m;
                  },1000);
              }
      
              //使用闭包,使用闭包的好处是把防抖封装成了函数。
              function debounce(fn,delay)
              {
                  let timer = null;
                  return function()
                  {
                      console.log("xxxxx");
                      if(timer !== null)
                      {
                          clearTimeout(timer);
                      }
                      timer = setTimeout(fn,delay);
                  }
              }
      
              button.onclick = debounce(() => {number.innerHTML = ++m},1000);
      
          </script>
      </html>
      
  • 节流

    • 节流,是指在触发事件之后,只执行第一次,然后在一段时间内,剩下的触发都不会执行事件。(用于即使用户频繁触发事件,也要求有一定反馈的场景)。

      <!DOCTYPE html>
      <html>
          <head>
              <meta charset = "utf-8"/>
              <link type = "text/css" rel = "stylesheet" href = "a.css"/>
              <style type = "text/css"></style>
          </head>
          <body>
              <h1>节流</h1>
              <input id = "btn" type = "button" value = "add"/>
              <h2>数字</h2>
              <span id = "num">0</span>
          </body>
          <script>
              let button = document.getElementById('btn');
              let number = document.getElementById('num');
      
              let m = 0;
              let time;
              let isOk = true;
      
              //不使用闭包,思路清晰。
              button.onclick = function()
              {
                  console.log("xxxxxx");
                  if(!isOk)
                  {
                      return;
                  }
                  number.innerHTML = ++m;
                  isOk = false;
                  time = setTimeout(() => {isOk = true;clearTimeout(time);},1000);
              }
      
              //使用闭包,把节流封装成函数
              function throttle(fn,delay)
              {
                  let isOk = true;
                  return function()
                  {
                      if(!isOk)
                      {
                          return;
                      }
                      isOk = false;
                      fn();
                      time = setTimeout(() => {
                          isOk = true;
                          clearTimeout(time);
                      },delay);
                  }
              }
      
              button.onclick = throttle(() => {number.innerHTML = ++m;},1000);
      
          </script>
      </html>
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值