• 前端经典面试题 | 理解 节流 和 防抖(后附手写节流\防抖)


    🖥️ 前端经典面试题专栏:前端经典面试题 | 理解节流和防抖
    🧑‍💼 个人简介:一个不甘平庸的平凡人🍬

    ✨ 个人主页:CoderHing的个人主页

    🍀 格言: ☀️ 路漫漫其修远兮,吾将上下而求索☀️

    👉 你的一键三连是我更新的最大动力❤️


    目录

    一、回答点

    二、深入回答

    节流和防抖的理解

    防抖应用场景:

    节流应用场景:

    实现节流和防抖

    简易版手撸节流函数

    简易版手撸防抖函数

    困难版手撸节流函数

    困难版手撸防抖函数


    一、回答点

    防抖:事件被触发n秒后执行回调

    节流:在规定一个时间段内,只能触发一次事件的回调函数

    二、深入回答

    节流和防抖的理解

    • 防抖:在事件被触发n秒后执行回调,如果在n秒内事件再次被触发,会重新计算时间;可使用在点击事件上,避免用户多次点击向后端发送多次网络请求.
    • 节流:在规定一个时间段内,只能触发一次事件的回调函数,如果在这个时间段内事件被多次触发,只会生效一次;可以用在页面滚动等事件监听上,通过节流来降低事件调用的频率.

    防抖应用场景:

    • 按钮:防止多次点击按钮,只执行一次.
    • 服务端:表单验证需服务端进行配合,只执行一段输入的事件的最后一次;搜索框的联想词.

    节流应用场景:

    • 拖拽:固定时间内只执行一次,防止高频率触发.
    • 缩放:监控resize.
    • 动画:固定时间内多次触发动画.

    实现节流和防抖

    简易版手撸节流函数

    1. function throttle(fn, delay) {
    2. let curTime = Date.now();
    3. return function() {
    4. let ctx = this,
    5. args = arguments,
    6. nowTime = Date.now();
    7. // 如果两次时间间隔超过了指定时间,则执行函数。
    8. if (nowTime - curTime >= delay) {
    9. curTime = Date.now();
    10. return fn.apply(ctx, args);
    11. }
    12. };
    13. }

    简易版手撸防抖函数

    1. function debounce(fn, wait) {
    2. let timer = null;
    3. return function() {
    4. let ctx = this,
    5. args = arguments;
    6. // 如果此时有定时器的话,取消之前的定时器重新记时
    7. if (timer) {
    8. clearTimeout(timer);
    9. timer = null;
    10. }
    11. // 设置定时器,让事件间隔指定时间后执行
    12. timer = setTimeout(() => {
    13. fn.apply(ctx, args);
    14. }, wait);
    15. };
    16. }

    困难版手撸节流函数

    1. function throttle(fn, interval, options = { leading: true, trailing:false }) {
    2. // 记录开始时间
    3. const { leading,trailing,resultCallBack } = options
    4. let endTime = 0
    5. let timer = null
    6. // 触发,执行函数
    7. const _throttle = function(...args) {
    8. return new Promise((resolve, reject) => {
    9. // 获取当前时间触发的时间
    10. const newTime = new Date().getTime()
    11. if (!endTime && !leading) endTime = newTime
    12. // 使用触发的事件和之前的时间间隔及开始时间,计算出 还剩多长时间需要去触发函数
    13. const remainTime = interval - (newTime - endTime)
    14. if (remainTime <= 0){
    15. if (timer) {
    16. clearTimeout(timer)
    17. timer = null
    18. }
    19. // 触发函数
    20. const result = fn.apply(this, args)
    21. if (resultCallBack) resultCallBack (result)
    22. resolve(result)
    23. // 保留上次触发时间
    24. endTime = newTime
    25. return
    26. }
    27. if (trailing && !timer) {
    28. timer = setTimeout(() => {
    29. timer = null
    30. endTime = !leading ? 0 : new Date().getTime()
    31. const result = fn.apply(this, args)
    32. if (resultCallBack) resultCallBack(result)
    33. resolve(resolve)
    34. },remainTime)
    35. }
    36. })
    37. }
    38. _throttle.cancel = function() {
    39. if(timer) clearTimeout(timer)
    40. timer = null
    41. endTime = 0
    42. }
    43. return _throttle
    44. }

    困难版手撸防抖函数

    1. function debounce(fn, delay, immediate = false, resultCallback) {
    2. // 1.定义一个定时器, 保存上一次的定时器
    3. let timer = null
    4. let invoke = false
    5. // 2.真正执行的函数
    6. const _debounce = function(...args) {
    7. return new Promise((resolve, reject) => {
    8. // 取消上一次的定时器
    9. if (timer) clearTimeout(timer)
    10. // 判断是否需要立即执行
    11. if (immediate && !invoke) {
    12. const res = fn.apply(this, args)
    13. if (resultCallback) resultCallback(res)
    14. resolve(res)
    15. invoke = true
    16. } else {
    17. // 延迟执行
    18. timer = setTimeout(() => {
    19. // 外部传入的真正要执行的函数
    20. const res = fn.apply(this, args)
    21. if (resultCallback) resultCallback(res)
    22. resolve(res)
    23. invoke = false
    24. timer = null
    25. }, delay)
    26. }
    27. })
    28. }
    29. // 取消功能
    30. _debounce.cancel = function() {
    31. console.log(timer)
    32. if (timer) clearTimeout(timer)
    33. timer = null
    34. invoke = false
    35. }
    36. return _debounce
    37. }
  • 相关阅读:
    2024的开放式耳机排行榜,看这六个耳机选购的小Tips
    使用树莓派学习PostgreSQL(二):通过MSSQL Linked Server导入数据
    【计算机视觉40例】案例20:K均值聚类实现艺术画
    MySQL数据库开发设计规范总结
    舵机云台实现体感姿态跟随功能
    A tour of gRPC:07 - gRPC bidirectional straming 双向流
    易点易动固定资产管理系统:2023年市场主流的选择
    关于HTTP协议的概述
    高品质工地建筑模板,防水耐用,易脱模
    杰理之MIC 免电容方案需要设置【篇】
  • 原文地址:https://blog.csdn.net/coderHing/article/details/127812331