当前位置:网站首页>【防抖与节流】
【防抖与节流】
2022-07-27 18:08:00 【逍码农】
事件的防抖
当一个函数在一定的事间内执行的次数太高了,我们就需要降低频率,如果在200ms以内没有再次触发滚动事件,那么久执行次函数 如果220ms内再次触发滚动事件,那么取消当前计时。重新开始计时。效果如果短时间内大量触发滚动只会执行一次函数 。我们可以用一下函数 /* * 参数 * fn:一个要执行的函数 * delay:延迟的时间 */ function debounce(fn,delay){ //首先创建一个变量来存储一个定时器 var timer=null; //下面这一行代码是闭包 return function(){ if(timer){ clearTimeout(timer) } timer=setTimeout(fn,delay) } }
事件的节流
function throttle(fn,delay){
var valid=true;
return function(){
if(!valid){
return valid=true
}
valid=false
setTimeout(function(){
fn();
valid=true;
},2000)
}
}边栏推荐
- MySQL learning record (III) multi table query, sub query, paging query, case statement, single line function
- CONDA common commands
- Clickhouse implements materializedpostgresql
- Mongodb learning notes: bson structure analysis
- 金仓数据库 Oracle至KingbaseES迁移最佳实践(2. 概述)
- Apple Mobile Bluetooth networking
- Standing on the shoulders of giants to learn, jd.com's popular architect growth manual was launched
- Linked list~~~
- [design tutorial] yolov7 target detection network interpretation
- A recently summarized universal violent cracking method
猜你喜欢

创新案例 | 本地生活服务数字化,高德品牌升级的增长战略

MySQL驱动jar包的下载--保姆教程

Redis basic understanding, five basic data types

antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
RK3399平台开发系列讲解(进程篇)15.36、理解进程和协程

Write bootloader from 0
![Leetcode:1498. Number of subsequences that meet the conditions [sort + bisection + power hash table]](/img/a9/de68e8affcb6b84e82cf344e7254e3.png)
Leetcode:1498. Number of subsequences that meet the conditions [sort + bisection + power hash table]

Flask Mdict builds online MDICT Dictionary Service

JVs official account login configuration

Arduino开发(二)_基于Arduino UNO开发板的RGB灯光控制方法
随机推荐
Unity fairygui play video (Lua)
How to optimize the open source community experience through developer metrics
Management of user organization structure
Understand │ what is cross domain? How to solve cross domain problems?
Lennix Lai, OKx financial market director: Web3 is a revolution
Linked list~~~
greedy
Pytorch multiplication and broadcasting mechanism
Interviewer: what is the abstract factory model?
软件测试面试题:字符串 “axbyczdj“,如果得到结果“abcd
Under the epidemic, I left my job for a year, and my income increased 10 times
JVs privatization deployment start failure handling scheme
How to improve the picture transmission speed and success rate in the development of IM instant messaging under the mobile network
Some contents related to cmsis-rtos
【分层强化学习】HAC论文及代码
Mongodb learning notes: bson structure analysis
面了个腾讯拿38K跳槽出来的,见识到了真正的测试天花板
antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
In 2019, China's smart machine Market: Huawei won nearly 4 components, firmly ranking first in China
【毕设教程】YOLOv7 目标检测网络解读