防抖和节流的区别以及代码实现

1、区别

防抖和节流的目的都是为了节省性能损耗。都是希望在一定的时间间隔内,不要重复触发请求。一般场景用在搜索和网页滚动事件中。他们的区别如下:

  • 防抖:在规定时间内执行函数。如果在这段时间内再次触发,则重新开始计时。
  • 节流: 在固定的时间执行函数。如果在这段时间内再次触发,不会重新开始计时。在这段时间只能执行一次。

通俗一点讲,防抖就比如说你准备学习60分钟,如果在此期间,你被外界因素打断了,你就重新开始学习60分钟。
别再打断我啦
节流就比如说你准备学习60分钟,如果在此期间,你被外界因素打断了,不管什么事你都置之不理,先把60分钟学习完。
专心学习

2.代码实现

    /**
     * @description: 防抖函数
     * @param {function} func 回调函数
     * @param {function} delay 间隔时间
     * @return: function
     */    
    function antiShake(func, delay) {
      let timer // 通过闭包的方式保存timer的值
      return function (...args) { 
       // 如果之前已经进入过了,timer就已经存在,清空定时器
        if (timer) { 
          clearTimeout(timer)
        }
       // 重新设置定时器  注:这是一次性定时器
        timer = setTimeout(() => {
          func.apply(this, args)
        }, delay);
      }
    }


    /**
     * @description: 节流函数
     * @param {function} func 回调函数
     * @param {function} wait 间隔时间
     * @return: function
     */    
    function throttling(func, wait) {
      let timer // 通过闭包的方式保存timer的值
      return function(...args) { 
        // 如果定时器已经存在  要等定时时间执行完才能重新执行
        if (!timer) {
          timer = setTimeout(() => {
            func.apply(this, args)
            timer = null 
          }, wait);
        }
      }
    }
    // 插入一个Input文本框
    document.querySelector('input').addEventListener('keyup', antiShake(function() 	 {
      console.log('只要你点的够快,我就不执行')
    }, 500))

    document.querySelector('html').addEventListener('mousemove', throttling(function() {
      console.log('1s只能执行一次')
    }, 1000))

仅仅是个人理解,如有不足之处,敬请指正。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
防抖节流都是为了解决频繁触发事件时的性能问题。 防抖(Debounce):在某个时间段内,只执行一次事件。比如当用户不断输入搜索框时,不会立即执行搜索操作,而是等用户停止输入一段时间后再执行搜索操作,避免频繁发送请求。 节流(Throttle):在某个时间段内,多次事件只执行一次。比如当用户不断滚动页面时,不会每滚动一次就触发事件,而是等待一段时间后再执行,避免频繁触发事件。 具体代码实现如下: 防抖: ```javascript function debounce(fn, delay) { let timer = null; return function() { const context = this; const args = arguments; clearTimeout(timer); timer = setTimeout(function() { fn.apply(context, args); }, delay); } } // 使用方式 const searchFn = debounce(function() { // 搜索操作 }, 500); input.addEventListener('input', searchFn); ``` 节流: ```javascript function throttle(fn, delay) { let timer = null; let lastTime = 0; return function() { const context = this; const args = arguments; const now = +new Date(); if (now - lastTime > delay) { clearTimeout(timer); lastTime = now; fn.apply(context, args); } else { clearTimeout(timer); timer = setTimeout(function() { lastTime = now; fn.apply(context, args); }, delay - (now - lastTime)); } } } // 使用方式 const scrollFn = throttle(function() { // 滚动事件 }, 500); window.addEventListener('scroll', scrollFn); ``` 以上是基于函数的方式实现防抖节流,也可以使用 Lodash 等第三方库来实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值