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

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中文官网

相关文章:

  • 从零开始的python学习(八)P108+P109+P110+P111+P112+P113+P114
  • 提升移动端网页调试效率:WebDebugX 与常见工具组合实践
  • WebGL与Three.js:从基础到应用的关系与原理解析
  • Web 架构之 API 安全防护:防刷、防爬、防泄漏
  • WEB3全栈开发——面试专业技能点P7前端与链上集成
  • 【CANN全新升级】CANN创新MLAPO算子,DeepSeek模型推理效率倍增
  • 如何用Coze+Fetch快速构建结构化文档
  • 在ARM+Ascend NPU上适配Step-Audio模型
  • JS红宝书笔记 10.6 - 10.10 函数
  • Android Framework 之 AudioDeviceBroker
  • 【论文阅读】大模型优化器(Large Language Models As Optimizers)
  • 全面掌握Pandas时间序列处理:从基础到实战
  • UE5 学习系列(二)用户操作界面及介绍
  • Vue 模板语句的数据来源
  • MybatisPlus枚举类的应用与转换
  • 六、接口关联
  • 【Kubernetes】Ingress-nginx快速入门
  • “概率鹦鹉”难解语义等价验证的NPC难题: 从技术本质看LLM在SQL优化任务中的致命缺陷
  • 【Java多线程从青铜到王者】单例设计模式(八)
  • TMC2226超静音步进电机驱动控制模块
  • 学校网站建设目的/自媒体运营主要做什么
  • 做招聘网站怎么样/品牌策划
  • 社群营销怎么做/站长工具seo排名
  • 招聘网站销售怎么做/湖北seo整站优化
  • 大型网站建设费用/百度竞价排名事件
  • 现在推广网站最好的方式/谷歌浏览器入口