当前位置:网站首页>JS中的防抖和节流
JS中的防抖和节流
2022-08-02 00:14:00 【weixin_46051260】
防抖(debounce)
在一定时间内,只执行最后一次任务。
在事件被触发的 N 秒后再执行任务(回调函数),如果在这 N 秒内又触发事件,则重新计时:
- 当事件触发时,相应的响应处理函数不会立即被触发,而是等待一定的时间;
- 当事件密集触发时,函数的触发会被频繁的推迟;
- 只有等待了一段时间也没有事件触发,才会真正执行响应处理函数。
防抖的应用场景:
- 手机号、邮箱输入检测
- 搜索框搜索输入(只需最后一次输入完后,再发送 Ajax 请求,减少请求次数,节约请求资源)
- 窗口大小 resize(只需窗口调整完成后,计算窗口大小,防止重复渲染)
- 滚动事件 scroll(只需执行触发的最后一次滚动事件的处理程序)
- 文本输入的验证(连续输入文字后发送 Ajax 请求进行验证,(停止输入后)验证一次就好
手写实现防抖:
var btn=document.querySelector('button')
var ipt=document.querySelector('input')
btn.addEventListener('click',debounce(getValue,2000))
function getValue(){
var val=ipt.value
console.log(val);
}
function debounce(fn,time){
let t=null
return function(){
if(t){
clearTimeout(t)
}
var firstClick=!t
if(firstClick){
fn.apply(this,arguments)
}
t=setTimeout(() => {
t=null
}, time);
}
}
节流
在一定时间内,同一个任务只会执行一次。
在一定时间内,只能触发一次响应函数,如果事件频繁触发,只有一次生效。
节流的应用场景:
- DOM 元素的拖拽功能实现(mousemove)
- 射击游戏的 mousedown/keydown 事件(单位时间只能发射一颗子弹)
- 计算鼠标移动的距离(mousemove)
- 搜索联想(keyup)
- 滚动事件 scroll,(只要页面滚动就会间隔一段时间判断一次)
var btn=document.querySelector('button')
var ipt=document.querySelector('input')
btn.addEventListener('click',throttle(getValue,2000))
function getValue(){
var val=ipt.value
console.log(val);
}
function throttle(fn,time){
var begin=0
return function(){
var date=new Date().getTime()
if(date-begin>time){
fn.apply(this,arguments)
begin=date
}
}
}
边栏推荐
- Detailed explanation of JSP request object function
- Short video SEO search operation customer acquisition system function introduction
- nodeJs--mime module
- 632. 最小区间
- 基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
- Play NFT summer: this collection of tools is worth collecting
- JSP out.print()和out.write()方法的不同之处
- The Statement update Statement execution
- JSP request对象功能详解说明
- Arduino Basic Syntax
猜你喜欢

协作乐高 All In One:DAO工具大全

632. Minimum interval

使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN

Realize deletion - a specified letter in a string, such as: the string "abcd", delete the "a" letter in it, the remaining "bcd", you can also pass multiple characters to be deleted, and pass "ab" can

短视频SEO搜索运营获客系统功能介绍

玩转NFT夏季:这份工具宝典值得收藏

Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation

Routing strategy

攻防世界-web-Training-WWW-Robots

如何发现新的潜力项目?工具推荐
随机推荐
测试用例:四步测试设计法
[Headline] Written test questions - minimum stack
MySQL常用语句整理
A simple file transfer tools
Redis - message publish and subscribe
[21-Day Learning Challenge] A small summary of sequential search and binary search
一文概览最实用的 DeFi 工具
不要用jOOQ串联字符串
NodeJs, all kinds of path
Pytorch seq2seq 模型架构实现英译法任务
具有通信时延的多自主体系统时变参考输入的平均一致性跟踪
632. Minimum interval
146. LRU cache
go笔记之——goroutine
How to use the go language standard library fmt package
An overview of the most useful DeFi tools
When Netflix's NFTs Forget Web2 Business Security
How does JSP use the page command to make the JSP file support Chinese encoding?
How to design a circular queue?Come and learn~
什么是低代码(Low-Code)?低代码适用于哪些场景?