手写防抖节流

article/2025/11/3 14:45:41

文章目录

  • 手写前端常用技巧-防抖节流
  • 防抖
  • 节流
    • 1. 首节流
    • 2. 尾节流
    • 3. 首尾节流
  • 总结


手写前端常用技巧-防抖节流

防抖:当持续触发事件时,一定时间内没有再触发事件,才会在一段时间之后触发事件处理函数。

节流:当持续触发事件时,保证一段delay之内,只调用一次函数

防抖

  • 源代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>防抖</title>
</head>
<body><label for='undebounce'>没防抖:</label><input type="text" id='undebounce' onInput='onInput1(event)' ><br /><label for="debounce">有防抖:</label><input type="text" id='debounce' onInput='debounceInput(event)' ><script>function debounce(fn, delay){let time = nullreturn function(...args){if(time){clearTimeout(time)}time = setTimeout(()=>{fn.apply(this, args)},delay)}}function onInput(e){val = e.target.valueif(val){console.log('有防抖',val)}}const debounceInput = debounce(onInput, 300)function onInput1(e){val = e.target.valueif(val){console.log('没有防抖',val)}}</script>
</body>
</html>
  • 执行结果

请添加图片描述

::: tip

  • 结果分析
  1. 没有防抖的输入框持续输入时,每键入一个字母都执行一次处理函数
  2. 有防抖的输入框持续输入时,在键入字母结束之后才执行一次处理函数

:::

节流

1. 首节流

首节流,时间戳的实现,首次触发立即执行,停止触发后,没办法再次执行

  • 源代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>节流throttle</title>
</head>
<body><ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
</body>
<style>body{display: flex;justify-content: center;}ul, li {padding:0;margin: 0;}li{width: 250px;height:200px;list-style: none;display: flex;justify-content: center;align-items: center;}li:nth-child(2n){background:whitesmoke;}li:nth-child(2n+1){background:yellow;}
</style>
<script>// 首节流,时间戳的实现,停止触发后,没办法再次执行function throttle(fn, interval){let prev = 0;return function(...args){const now = Date.now();if( now-prev >= interval ){prev = nowfn.apply(this, args)}}}function handle(){console.log(new Date())}const throttleHandler = throttle(handle, 3000)console.log('tt', throttleHandler)window.addEventListener('scroll', throttleHandler)
</script>
</html>

请添加图片描述

可以看到,只要滑动了,就会触发事件,但是停止滑动之后,不会触发最后一次。

2. 尾节流

定时器实现,不会立即执行,而是在delay之后执行

最后停止触发之后,还会执行一次

  • 源代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>节流throttle</title>
</head>
<body><ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
</body>
<style>body{display: flex;justify-content: center;}ul, li {padding:0;margin: 0;}li{width: 250px;height:200px;list-style: none;display: flex;justify-content: center;align-items: center;}li:nth-child(2n){background:whitesmoke;}li:nth-child(2n+1){background:yellow;}
</style>
<script>// 尾节流,定时器实现,不会立即执行,而是在delay之后执行// 最后停止触发之后,还会执行一次function throttle(fn,delay){let timer = nullreturn function(...args){if( !timer ){timer = setTimeout(()=>{fn.apply(this, args)timer = null}, delay)}}}function handle(){console.log(new Date())}const throttleHandler = throttle(handle, 3000)console.log('tt', throttleHandler)window.addEventListener('scroll', throttleHandler)
</script>
</html>

请添加图片描述

可以看到我们停止滑动之后还是触发了事件

3. 首尾节流

  • 源代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>节流throttle</title>
</head>
<body><ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
</body>
<style>body{display: flex;justify-content: center;}ul, li {padding:0;margin: 0;}li{width: 250px;height:200px;list-style: none;display: flex;justify-content: center;align-items: center;}li:nth-child(2n){background:whitesmoke;}li:nth-child(2n+1){background:yellow;}
</style>
<script>// 首尾节流,计时器+时间戳function throttle(fn, delay){let timer = nulllet startTime = 0return function(...args){let curTime = Date.now();let remaining = delay - (curTime - startTime)// 清除旧的计时器clearTimeout(timer)if(remaining <= 0 ){fn.apply(this, args)startTime = Date.now()}else{timer = setTimeout(()=>{fn.apply(this, args)startTime = Date.now()}, remaining)}}}function handle(){console.log(new Date())}const throttleHandler = throttle(handle, 500)console.log('tt', throttleHandler)window.addEventListener('scroll', throttleHandler)
</script>
</html>

请添加图片描述

在这里我们缩短节流时间,方便看效果。

可以看到每次滑动都至少触发两次事件,即首尾触发

总结

虽然防抖和节流都是避免同一时间频繁执行处理函数,但是原理却有一些差别

::: note

应用场景

  • 防抖 debounce

    • search搜索联想,用户在不断输入值时,用防抖来节约请求资源。
    • window触发resize的时候,不断的调整浏览器窗口大小会不断的触发这个事件,用防抖来让其只触发一次
  • 节流 throttle

    • 鼠标不断点击触发,mousedown(单位时间内只触发一次)
    • 监听滚动事件,比如是否滑到底部自动加载更多,用throttle来判断

:::

文章详细信息,请查看个人博客


http://chatgpt.dhexx.cn/article/2fke5fDx.shtml

相关文章

防抖和节流

1. 什么是防抖 防抖策略&#xff08;debounce&#xff09;是当事件被触发后&#xff0c;延迟 n 秒后再执行回调&#xff0c;如果在这 n 秒内事件又被触发&#xff0c;则重新计时。 1.2. 防抖的应用场景 用户在输入框中连续输入一串字符时&#xff0c;可以通过防抖策略&…

lodash节流

滚动条事件优化 可以用 lodash节流 npm i -S lodash Lodash 简介 | Lodash 中文文档 | Lodash 中文网

JS 节流

JS 节流 说明: 1.对于高频触发的监听事件函数,实现对于触发次数的间接限制,从而降低触发次数. 2.关键点在于控制时间周期内,阻止触发内容,即上锁;在时间周期外解锁,触发内容。 3.主要是对间隔时间限制,在规定时间内,阻止触发事件内指定程序或默认抛弃 4.三种实现节流方式:时间…

个人对于节流的理解!

文章目录 前言一、节流是什么&#xff1f;二、节流的实现总结 前言 防抖和节流是前端经常会被提起以及涉及到的内容&#xff0c;更是前端性能优化的手段之一&#xff0c;我初学防抖和节流也遭遇了很多坑&#xff0c;所以想写一篇博客一则当作学习笔记&#xff0c;二则如果能帮…

节流的基本使用以及理解

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 一、节流是什么&#xff1f;应用场景 二、使用步骤1.定义节流阀2.绑定 mousemove 事件3. 判断节流阀是否为空&#xff0c;如果不为空,说明距离上一次执行时间还没有…

图片跟随鼠标样式跟随效果(附完整代码及效果)

Demo效果如下&#xff1a; 完整代码如下&#xff1a; <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><meta …

前端必备技能之----节流

&#xff08;引言----和大佬们出去吃饭总是会有收获的&#xff0c;这个知识点是我之前从未考虑过的事情&#xff0c;但是在现代的设计开发之中却是非常重要且使用频率非常之高的两个概念。&#xff09; 作为一个前端的初学者&#xff0c;因为之前淋过雨&#xff0c;所以想为同…

html锚点链接小案例

案例1&#xff1a;回到首页 <html ><head><meta charset"utf-8"><title></title><style type"text/css"> *{border: 0;margin: 0; padding: 0;}.box1, .box2{height: 3000px; width: 200px;background-color: green…

HTML锚点为什么叫hash,锚点链接和hash属性

相信大家挺经常见过这样一个效果。有一个很长很长的页面,分成好几部分,目录中一点击,就能定位到页面某个位置。 例如:有这样一个目录,例如你点击一下“HTML”,就会直接跳转到“HTML”的页面位置 这就是锚点链接(也叫书签链接),常常用于那些内容庞大繁琐的网页,通过点击…

html5添加锚点锭接,为页面添加锚点链接

为页面添加锚点链接 开哈础是发通待质击文以为近哈知按分过续的战发中会遇到为页面中添加锚点链接的需求,即在页面中点击某处,可以跳转到与之有联系的地方。添加锚点的方法比较多,在这儿,把常用的方法大享上。是发了概开程态间些告人屏果会区。一一是控标近体到班都一从小述…

菜鸟 html锚链接,Vue锚点链接

锚点链接是我们在开发中经常会用到的一个技术点&#xff0c;常见的常见有&#xff0c;页面内容过多&#xff0c;而我们不希望拿鼠标一直来回滚动&#xff0c;就需要用到锚点链接&#xff0c;以 " 目录 " 的方式来进行对应的跳转。 而在常见的项目中&#xff0c;锚点链…

制作图片锚点链接html,锚点链接怎么做

网页内容过多时我们可以使用锚点链接来进行位置的跳转&#xff0c;通过锚点链接我们不但可以指向文档&#xff0c;还能指向页面里的特定段落&#xff0c;这样就会便于我们来浏览网页中的内容&#xff0c;那么&#xff0c;锚点链接怎么实现呢&#xff1f;本篇文章就来给大家介绍…

web前端学习26(锚点链接)

文章目录 4.7.2 链接分类 4.7.2 链接分类 锚点链接&#xff1a;点我们点击链接&#xff0c;可以快速定位到页面中的某个位置。 在链接文本的href属性中&#xff0c;设置属性值为#名字的形式&#xff0c;如< a href"#two">第2集< /a> 点完这个链接就会跳…

Web容器版本泄露漏洞修复

0x00 背景 恶意攻击者可以根据版本信息寻找相关漏洞&#xff0c;进行利用漏洞攻击 0x01 修复思路 通过修改配置或者配置错误提示页面&#xff0c;隐藏 web容器的版本号及其它敏感信息。 0x02 代码修复 Apache 版本号 隐藏 Apache 的版本号及其它敏感信息&#xff0c;配置操…

Spring容器 SpringMVC容器 web容器的关系

说到spring和springmvc&#xff0c;其实有很多工作好多年的人也分不清他们有什么区别&#xff0c;如果你问他项目里用的什么MVC技术&#xff0c;他会说我们用的spring和mybatis&#xff0c;或者spring和hibernate。 在潜意识里会认为springmvc就是spring&#xff0c;之前我也是…

SpringBoot深入(一)--SpringBoot内置web容器及配置

版权声明&#xff1a;作者原创&#xff0c;转载请注明出处。 本系列文章目录地址&#xff1a;http://blog.csdn.net/u011961421/article/details/79416510 前言 在学会基本运用SpringBoot同时&#xff0c;想必搭过SSH、SSM等开发框架的小伙伴都有疑惑&#xff0c;SpringBoot在…

SpringBoot内置web容器及配置

前言 在学会基本运用SpringBoot同时&#xff0c;想必搭过SSH、SSM等开发框架的小伙伴都有疑惑&#xff0c;SpringBoot在spring的基础上做了些什么&#xff0c;使得使用SpringBoot搭建开发框架能如此简单&#xff0c;便捷&#xff0c;快速。本系列文章记录博主网罗博客、分析源码…

Web 容器、HTTP 服务器 、Servlet 容器区别与联系

首先浏览器发起 HTTP 请求&#xff0c;像早期的时候只会请求一些静态资源&#xff0c;这时候需要一个服务器来处理 HTTP 请求&#xff0c;并且将相应的静态资源返回。 这个服务器叫 HTTP 服务器。 简单点说就是解析请求&#xff0c;然后得知需要服务器上面哪个文件夹下哪个名字…

常见的几种web容器(Apache、Nginx、Tomcat)

目录 前言ApacheNginxTomcat ~~~~~~~~ 因为想要面对一个新的开始&#xff0c;一个人必须有梦想、有希望、有对未来的憧憬。如果没有这些&#xff0c;就不叫新的开始&#xff0c;而叫逃亡。 ​​​​ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~…

web容器、中间件以及web服务器的区别

一、web容器 1.web容器的介绍 web容器是一种服务程序&#xff0c;在服务器一个端口就有一个提供相应服务的程序&#xff0c;而这个程序就是处理从客户端发出的请求。实际上&#xff0c;Web容器是一种服务程序&#xff0c;给处于其中的应用程序组件提供环境&#xff0c;使其直接…