如何使用 js 实现一个 debounce 函数

  1. 原理

防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时!

  1. 实现方式

  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-10-01
  8. * @modified
  9. *
  10. * @description 防抖 & debounce
  11. * @difficulty Easy Medium Hard
  12. * @complexity O(n)
  13. * @augments
  14. * @example
  15. * @link
  16. * @solutions
  17. *
  18. * @best_solutions
  19. *
  20. */
  21. const log = console.log;
  22. // 防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时!
  23. function debounce(callback, timer = 1000) {
  24. let id = null;
  25. return function() {
  26. clearTimeout(id);
  27. id = setTimeout(() => {
  28. callback();
  29. }, timer);
  30. }
  31. }
  32. const cb = () => log(`callback function!`)
  33. const test = debounce(cb, 3000);
  34. log(`test`, test);
  35. test();
  36. setTimeout(() => {
  37. log(`test2`);
  38. test();
  39. }, 1000);
  40. setTimeout(() => {
  41. log(`test3`);
  42. test();
  43. }, 2000);
  44. /*
  45. $ node debounce.js
  46. test [Function]
  47. test2
  48. test3
  49. callback function!
  50. */

this & arguments


  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-10-01
  8. * @modified
  9. *
  10. * @description 防抖 & debounce
  11. * @difficulty Easy Medium Hard
  12. * @complexity O(n)
  13. * @augments
  14. * @example
  15. * @link
  16. * @solutions
  17. *
  18. * @best_solutions
  19. *
  20. */
  21. const log = console.log;
  22. // 防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时!
  23. function debounce(callback, timer = 1000) {
  24. let id = null;
  25. return function() {
  26. // const args = [...arguments];
  27. const args = arguments;
  28. const that = this;
  29. // function (this, arguments)
  30. clearTimeout(id);
  31. id = setTimeout(function () {
  32. log(`that`, that)
  33. log(`this`, this)
  34. log(`args`, args)
  35. callback.call(that, [...args]);
  36. }, timer);
  37. // Arrow Function (this)
  38. // id = setTimeout(() => {
  39. // callback();
  40. // }, timer);
  41. }
  42. }
  43. // const cb = () => log(`callback function!`);
  44. const cb = (args) => log(`callback function!`, args);
  45. const test = debounce(cb, 3000);
  46. log(`test`, test);
  47. test(`args = arguments`, 1);
  48. setTimeout(() => {
  49. log(`test2`);
  50. test(`args = arguments`, 2);
  51. }, 1000);
  52. setTimeout(() => {
  53. log(`test3`);
  54. test(`args = arguments`, 3);
  55. }, 2000);
  56. /*
  57. $ node debounce.js
  58. test [Function]
  59. test2
  60. test3
  61. callback function!
  62. */
  63. /*
  64. $ node debounce.js
  65. test [Function]
  66. test2
  67. test3
  68. that undefined
  69. this Timeout {
  70. _idleTimeout: 3000,
  71. _idlePrev: null,
  72. _idleNext: null,
  73. _idleStart: 2044,
  74. _onTimeout: [Function],
  75. _timerArgs: undefined,
  76. _repeat: null,
  77. _destroyed: false,
  78. [Symbol(refed)]: true,
  79. [Symbol(asyncId)]: 11,
  80. [Symbol(triggerId)]: 7
  81. }
  82. args [Arguments] { '0': 'args = arguments', '1': 3 }
  83. callback function! [ 'args = arguments', 3 ]
  84. */
  1. 总结

refs

https://www.cnblogs.com/xgqfrms/p/11886342.html

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


如何使用 js 实现一个 debounce 函数的更多相关文章

  1. 如何使用 js 实现一个 throttle 函数

    如何使用 js 实现一个 throttle 函数 原理 实现方式 "use strict"; /** * * @author xgqfrms * @license MIT * @c ...

  2. 如何用 js 实现一个 apply 函数

    如何用 js 实现一个 apply 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen ...

  3. 如何用 js 实现一个 call 函数

    如何用 js 实现一个 call 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...

  4. 如何用 js 实现一个 sleep 函数

    如何用 js 实现一个 sleep 函数 原理 实现方式 总结 refs js sleep xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  5. 如何用 js 实现一个 new 函数

    如何用 js 实现一个 new 函数 原理 new 关键字实现经过了如下过程 创建一个空对象 obj = {} 链接到原型 obj.proto = constructor.prototype 绑定 t ...

  6. 如何用 js 实现一个 bind 函数

    如何用 js 实现一个 bind 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...

  7. 使用原生JS封装一个动画函数

    最近一直在忙项目,很少有时间回顾之前的知识,今天刚好要做一个轮播,因为对兼容性有一定的要求,使用了各种插件和库中的轮播,效果都不是很理想,一怒之下,使用原生JS封装了一个轮播组件,其中重要的功能就是一 ...

  8. JS魔法堂:函数节流(throttle)与函数去抖(debounce)

    一.前言 以下场景往往由于事件频繁被触发,因而频繁执行DOM操作.资源加载等重行为,导致UI停顿甚至浏览器崩溃. 1. window对象的resize.scroll事件 2. 拖拽时的mousemov ...

  9. underscore.js中的节流函数debounce及trottle

    函数节流   throttle and debounce的相关总结及想法 一开始函数节流的使用场景是:放止一个按钮多次点击多次触发一个功能函数,所以做了一个clearTimeout setTimeou ...

随机推荐

  1. (转载)微软数据挖掘算法:Microsoft Naive Bayes 算法(3)

    介绍: Microsoft Naive Bayes 算法是一种基于贝叶斯定理的分类算法,可用于探索性和预测性建模. Naïve Bayes 名称中的 Naïve 一词派生自这样一个事实:该算法使用贝叶 ...

  2. 单点登录(SSO)的设计与实现

    一.前言 1.SSO说明 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.https://baike.baidu.c ...

  3. python基础(数据类型,while,if)

    python基础初识. 1,运行python代码. 在d盘下创建一个t1.py文件内容是: print('hello world') 打开windows命令行输入cmd,确定后 写入代码python ...

  4. loj10171

    牧场的安排 内存限制:512 MiB 时间限制:1000 ms 原题来自:USACO 2006 Nov. Gold Farmer John 新买了一块长方形的牧场,这块牧场被划分成 MMM 行 NNN ...

  5. java打exe

    参考文章: 注册码: https://www.cnblogs.com/jepson6669/p/9211208.html 官网: https://exe4j.apponic.com/ 在上篇基础上,将 ...

  6. jQuery——选择元素

    ###理解DOM jQuery最强大的特性之一就是能够简化在DOM中选择元素的任务.**DOM(文档对象模型)**充当了JavaScript与网页之间的接口,它以对象网络而非纯文本的形式来表现HTML ...

  7. linux查看log

    TOMCAT_PATH='/home/jyapp/apache-tomcat-7.0.59/'PID=`ps -ef |grep $TOMCAT_PATH | grep -v grep |awk '{ ...

  8. Java 复习整理day08

    package com.it.demo02_lambda; //接口, 表示动物. //public abstract class Animal { //报错, Lambda表达式只针对于接口有效 p ...

  9. [SpringSecurity] UserDetailsService 详解

    UserDetailsService 接口 当什么也没有配置的时候,账号和密码是由 Spring Security 定义生成的. 而在实际项目中账号和密码都是从数据库中查询出来的. 所以我们要通过自定 ...

  10. vue-cli-----vue实例中template: '<App/>',这样写是什么意思

    我刚开始学,看这个东西看了好久,官网文档的描述我不太理解,今天终于算明白了 官网的描述: 模板将会替换挂载的元素.挂载元素的内容都将被忽略 也就是说:template: '<App/>' ...