当前位置:网站首页>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
}
}
}
边栏推荐
- 已知中序遍历数组和先序遍历数组,返回后序遗历数组
- How to use the go language standard library fmt package
- 思维导图,UML在线画图工具
- 短视频SEO优化教程 自媒体SEO优化技巧方法
- 146. LRU cache
- 460. LFU cache
- Looking back at 5 recessionary times in history: How might this time be different?
- swing的Jlist列表滚动条以及增加元素的问题
- Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
- Industrial control network intrusion detection based on automatic optimization of hyperparameters
猜你喜欢

Microsoft PC Manager V2.1 beta version officially released

An interesting project--Folder comparison tool (1)

nodeJs--mime module

How to design a circular queue?Come and learn~

Collection of NFT tools

poker question
![[21-Day Learning Challenge] A small summary of sequential search and binary search](/img/81/7339a33de3b9e3aec0474a15825a53.png)
[21-Day Learning Challenge] A small summary of sequential search and binary search

CVPR 2022 | SharpContour:一种基于轮廓变形 实现高效准确实例分割的边缘细化方法

Short video seo search optimization main content

When Netflix's NFTs Forget Web2 Business Security
随机推荐
含外部储能的电力系统暂态稳定分布式控制
22. The support vector machine (SVM), gaussian kernel function
NodeJs, all kinds of path
构造方法,this关键字,方法的重载,局部变量与成员变量
思维导图,UML在线画图工具
els block deformation
业务测试如何避免漏测 ?
What is it like to trade for a living?
Simpson's paradox
go语言标准库fmt包怎么使用
460. LFU cache
JSP out. The write () method has what function?
An overview of the most useful DeFi tools
测试点等同于测试用例吗
鲲鹏编译调试插件实战
JSP如何使用page指令让JSP文件支持中文编码呢?
poker question
When Netflix's NFTs Forget Web2 Business Security
IP Core: FIFO
els block boundary deformation processing