当前位置: 首页 > web >正文

JS - 函数防抖详解

目录

  • 一、使用场景
  • 二、实现思路(详解,es6之前版本)
  • 三、es6实现
  • 四、第三方库

一、使用场景

  • 函数内执行耗时操作
  • 函数执行不频繁,只有最后一次有意义
  • 举例:鼠标移动事件,键盘输入事件…等需要逻辑处理时

二、实现思路(详解,es6之前版本)

举例场景为键盘输入事件,函数内部通过apply改变this指向,通过slice处理arguments(参数集)伪数组

  1. 前置
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>函数防抖</title>
</head>
<body><input type="text">
</body>
<script>var input = document.querySelector('input');input.addEventListener('input', function(e) {console.log(e.target.value);});
</script>
</html>
  1. 实现延时触发效果 – 使用高阶函数思想封装函数
    高阶函数(至少满足以下条件之一):1.接受一个或多个函数作为参数。2.返回一个新的函数。
	function debounce(fn, delay) {return function() {setTimeout(() => {fn();}, delay);}}var newFn = debounce(function() {console.log(111);}, 1000);var input = document.querySelector('input');input.addEventListener('input', newFn);
  1. 实现只响应最后一次触发 – 利用setTimeout返回值进行处理
    这里将this改为打印内容,我们可以发现this为undefined。
    原因:函数的this指向调用者,那么当前fn是在setTimeout中调用,指向全局
	function debounce(fn, delay) {var timer = null;return function() {clearTimeout(timer);timer = setTimeout(() => {fn();}, delay);}}var newFn = debounce(function() {console.log(this);}, 1000);var input = document.querySelector('input');input.addEventListener('input', newFn);
  1. 实现this绑定和参数传递
    arguments:函数调用时传入的所有参数(伪数组)
	function debounce(fn, delay) {var timer = null;return function() {clearTimeout(timer);timer = setTimeout(() => {var args = Array.prototype.slice.call(arguments,0); // 伪数组转化fn.apply(this,args);}, delay);}}var newFn = debounce(function(e) {console.log(this.value,e);}, 1000);var input = document.querySelector('input');input.addEventListener('input', newFn);
  1. 自定义参数传递
	function debounce(fn, delay) {var timer = null;return function() {clearTimeout(timer);timer = setTimeout(() => {var args = Array.prototype.slice.call(arguments,0);fn.apply(this,args);}, delay);}}var newFn = debounce(function(e,a) {console.log(e,a);}, 1000);var input = document.querySelector('input');input.addEventListener('input', (e)=>{newFn(e,1);});

三、es6实现

使用箭头函数、解构赋值、剩余参数等现代 JavaScript 特性
!!!箭头函数没有自己的 this,它会继承定义时所在作用域的 this

	const debounce = (fn, delay) => {let timer = null;return function(...args) {clearTimeout(timer);timer = setTimeout(() => {fn.apply(this, args);}, delay);}}const newFn = debounce(function(e) {console.log(e, this.value);}, 1000);const input = document.querySelector('input');input.addEventListener('input', newFn);

四、第三方库

lodash中文官网

http://www.xdnf.cn/news/13060.html

相关文章:

  • React 第五十八节 Router中StaticRouterProvider的使用详解及案例
  • 如何在服务器上部署 Python Django 应用
  • 打开网页即可远程控制手机,Linux系统亦可使用
  • c++学习之路1-安装部署opencv环境c++版本用visual studio
  • C#模式匹配深度解析与最佳实践
  • day49python打卡
  • MYSQL数据库
  • LeetCode 高频 SQL 50 题(基础版)之 【高级字符串函数 / 正则表达式 / 子句】· 下
  • VSCode 使用CMake 构建 Qt 5 窗口程序
  • 【C++特殊工具与技术】优化内存分配(四):定位new表达式、类特定的new、delete表达式
  • [论文阅读]人工智能 | CoMemo:给大视觉语言模型装个“图像记忆”
  • (37)课56--??:建立保存点 SAVEPOINT spA,回滚(至保存点) ROLLBACK (to spA)及综合举例。
  • 记录:RK3588 PWM调试
  • Unity UGUI Button事件流程
  • AlgorithmVisualizer项目改进与部署-网页算法可视化
  • 【学习记录】使用 Kali Linux 与 Hashcat 进行 WiFi 安全分析:合法的安全测试指南
  • ConcurrentModificationException 并发修改异常详解
  • 用递归算法解锁「子集」问题 —— LeetCode 78题解析
  • 代码随想录算法训练营第60期第六十三天打卡
  • 华硕a豆14 Air香氛版,美学与科技的馨香融合
  • vue+cesium示例:3D热力图(附源码下载)
  • pycharm 设置环境出错
  • matlab时序预测并绘制预测值和真实值对比曲线
  • 浏览器指纹科普 | Do Not Track 是什么?
  • 2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面
  • (14)-java+ selenium->元素定位大法之By xpath上卷
  • aurora与pcie的数据高速传输
  • 【从零学习JVM|第三篇】类的生命周期(高频面试题)
  • 自然语言处理——卷积神经网络
  • 你应该使用的 php 加解密函数