介绍防抖节流原理、区别以及应用,并用JavaScript进行实现

防抖函数

实现原理

在事件被触发 n 秒后再执行回调,如果在这 n 秒内又被触发,则重新计时。

适用场景

按钮提交场景**:防止多次提交按钮,只执行最后提交的一次
搜索框联想场景**:防止联想发送请求,只发送最后一次输入

 <button onclick="func()">按钮</button>
 /**
   * @param {Function} func 需要防抖处理的函数
   * @param {Number} wait 时间间隔
   * @return {Function} 将 debounce 处理结果当作函数返回
   */
  function debounce(func, wait) {
      // 通过闭包缓存一个定时器 id
      let timeout;
      // 将 debounce 处理结果当作函数返回
      // 触发事件回调时执行这个返回函数
      return function () {
          const context = this
          const args = arguments
          // 如果已经设定过定时器就清空上一次的定时器
          if (timeout) clearTimeout(timeout)
          // 开始设定一个新的定时器,定时器结束后执行回调函数 func
          timeout = setTimeout(function () {
              func.apply(context, args)
          }, wait)
      }
  }
// 执行 debounce 函数返回新函数
  const func = debounce(() => {
      console.log("func 防抖函数执行了" + Date.now())
  }, 1000)

  // 停止滑动 1 秒后执行函数 
  document.addEventListener('scroll', func)

vue3 实现防抖

<template>
  <el-button type="primary" @click="debounce($event)">primary</el-button>
  <!-- <div @click="debounce($event)">122</div> -->
</template>

<script lang="ts" setup name="debounce">
import { ref } from "vue";
let timer = ref("") as any;
function debounce(_, immediated = false, timeout = 2000) {
  return (function () {
    console.log(timer.value);
    console.log(immediated);
    if (immediated) {
      console.log("执行回调12");
      return;
    }
    if (timer.value) clearTimeout(timer.value);
    timer.value = setTimeout(() => {
      console.log("执行回调");
    }, timeout);
  })();
}
</script>

<style></style>

节流函数

实现原理

规定在一个单位时间内,只能触发一次函数。如果这个单位时间内触发多次函数,只有一次生效。

适用场景

  • 拖拽场景:固定时间内只执行一次,防止超高频次触发位置变动
  • 缩放场景:监控浏览器 resize

代码实现

 // 节流
    dothrotle(wait = 2000) {
      if(!this.flag) return
      if(this.flag){
         console.log(1133);
         this.flag = false
      }      

      this.timer = setTimeout(() => {
        this.flag = true
      }, wait);
    },

vue3实现

<template>
  <el-button type="primary" @click="throtle($event)">throtle</el-button>
</template>

<script lang="ts" setup name="throtle">
import { ref, unref } from "vue";
let flag = ref(true);
function throtle(_, immediated = false, timeout = 2000) {
  return (function () {
    console.log(immediated);
    if (immediated) {
      console.log("执行回调12");
      return;
    }
    console.log(unref(flag));
    if (!flag.value) return;
    flag.value = false;
    setTimeout(() => {
      flag.value = true;
      console.log("执行回调");
    }, timeout);
  })();
}
</script>

<style></style>

项目上封装使用的防抖函数

/**
 * 简易防抖函数
 * @param {Function} func -防抖目标函数
 * @param {Number}} gap - 防抖时间间隔
 */
export const debounce = ( func, gap ) => {
  let timer
  return function () {
    timer && clearTimeout( timer )
    timer = setTimeout( () => {
      func.apply( this, arguments )
    }, gap )
  }
}

有需要的请私信博主,还请麻烦给个关注,博主不定期更新,或许能够有所帮助!!请关注公众号

在这里插入图片描述

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@逆风boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值