当前位置:网站首页>js的防抖和节流
js的防抖和节流
2022-07-02 06:23:00 【江河i】
防抖和节流概念
防抖和节流都是性能优化的方式 : 针对的是高频触发的事件: 比如 滚动事件scroll, 用户输入事件 input
函数防抖
debounce:用户一直触发事件,事件处理函数是不会执行的,直到用户在指定的时间内不在触发该事件,则执行一次事件处理程序(防抖意味着事件处理程序只会执行性一次)
函数节流 throttle:用户一直触发事件,事件处理函数会每间隔指定的时间后执行一次, 在指定的时间内不会反复执行;(节流意味着事件处理程序每间隔指定的时间执行一次)
防抖和节流的区别:防抖是将多次执行变为最后一次执行,节流是将多次执行变为每隔一段时间执行
防抖和节流的实现方式: 计时器和时间戳 (为了防止变量污染,通过闭包的形式实现功能)
参考网址: https://www.cnblogs.com/youma/p/10559331.html
防抖实现(计时器实现)
function debounce(fn, delay) {
//fn就是真正事件处理的函数/delay是时间
// 提前声明一个计时器, 赋值为null
let timer = null;
return function() {
//最初想法
// if (!timer) {
// timer = setTimeout(fn, delay)
// } else {
// clearTimeout(timer);
// timer = setTimeout(fn, delay)
// }
//精简版 防抖: 关键点在于清除计时器的节点
// if (timer) {
// clearTimeout(timer);
// }
// timer = setTimeout(fn, delay)
// 考虑到this指向和传参的问题
let that = this;
let args = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function() {
fn.apply(that, args)
}, delay)
}
}
// 真正的时间处理程序
function print() {
console.log('hello world');
}
window.onscroll = debounce(print, 2000);
//当滚动的时候,调用一个函数
</script>
节流实现(时间戳实现)
<script>
//这个节流函数利用时间戳让第一次滚动事件执行一次回调函数,此后每隔1000ms执行一次,
//在小于1000ms这段时间内的滚动是不执行的
// 节流函数 时间戳
function throttle(fn, delay) {
// 起点的时间
let start = Date.now();
return function() {
// 获取当前时间,通过 当前时间 - 起点时间 = 时间差,
// 判断 时间差和 delay的关
var now = Date.now();
if (now - start >= delay) {
print()
// 事件处理程序执行一次后,需要重新计时
start = Date.now();
}
}
}
// 真正的事件处理程序
function print() {
console.log('节流');
}
window.onscroll = throttle(print, 1000);
</script>
如果要传参,改变this的指向
<script>
// 节流函数 时间戳
function throttle(fn, delay) {
// 起点的时间
let start = Date.now();
return function() {
let that = this; //this指向window
let args = arguments;
// 获取当前时间,通过 当前时间 - 起点时间 = 时间差,
// 判断 时间差和 delay的关系
let diff = Date.now() - start
if (diff > delay) {
fn.apply(this, arguments)
// 事件处理程序执行一次后,需要重新计时,所以获取现在的时间
start = Date.now();
}
}
}
// 真正的事件处理程序
function print() {
console.log('节流');
}
window.onscroll = throttle(print, 1000);
</script>
边栏推荐
- AWD learning
- web自动化切换窗口时报错“list“ object is not callable
- Vector types and variables built in CUDA
- Linux MySQL 5.6.51 community generic installation tutorial
- (第一百篇BLOG)写于博士二年级结束-20200818
- Asynchronous data copy in CUDA
- Redis——大Key問題
- Record RDS troubleshooting once -- RDS capacity increases dramatically
- Selenium+msedgedriver+edge browser installation driver pit
- CUDA user object
猜你喜欢
Detailed definition of tensorrt data format
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
Alibaba cloud MFA binding Chrome browser
Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
查询GPU时无进程运行,但是显存却被占用了
华为MindSpore开源实习机试题
Code skills - Controller Parameter annotation @requestparam
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
自学table au
随机推荐
Introduce two automatic code generators to help improve work efficiency
实现strStr() II
PgSQL学习笔记
Vector types and variables built in CUDA
js中正则表达式的使用
2020-9-23 QT的定时器Qtimer类的使用。
ZZQ的博客目录--更新于20210601
Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题
Redis——Cluster数据分布算法&哈希槽
[self cultivation of programmers] - Reflection on job hunting Part II
Linux MySQL 5.6.51 community generic installation tutorial
Blog directory of zzq -- updated on 20210601
kali最新更新指南
Linked list (linear structure)
ModuleNotFoundError: No module named ‘jieba.analyse‘; ‘jieba‘ is not a package
AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
Redis——热点key问题
Sentinel rules persist to Nacos
PIP install