当前位置:网站首页>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>
边栏推荐
- 阿里云MFA绑定Chrome浏览器
- The intern left a big hole when he ran away and made two online problems, which made me miserable
- Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
- Blog directory of zzq -- updated on 20210601
- 默认google浏览器打不开链接(点击超链接没有反应)
- pytest(2) mark功能
- kali最新更新指南
- selenium+msedgedriver+edge浏览器安装驱动的坑
- Sublime Text 配置php编译环境
- ModuleNotFoundError: No module named ‘jieba.analyse‘; ‘jieba‘ is not a package
猜你喜欢

代码技巧——Controller参数注解@RequestParam

Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决

Utilisation de la carte et de foreach dans JS

Redis - cluster data distribution algorithm & hash slot

查询GPU时无进程运行,但是显存却被占用了

一口气说出 6 种实现延时消息的方案

Pytest (1) case collection rules

Thread hierarchy in CUDA

Fe - wechat applet - Bluetooth ble development research and use

The intern left a big hole when he ran away and made two online problems, which made me miserable
随机推荐
No process runs when querying GPU, but the video memory is occupied
Fe - use of weex development weex UI components and configuration use
Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
Sentinel规则持久化到Nacos
Redis——大Key问题
查询GPU时无进程运行,但是显存却被占用了
由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
构建学习tensorflow
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
Functions of tensorrt
20201025 visual studio2019 qt5.14 use of signal and slot functions
js中正则表达式的使用
Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
2020-9-23 QT的定时器Qtimer类的使用。
Codeforces Round #797 (Div. 3) A—E
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
Code skills - Controller Parameter annotation @requestparam
ctf-web之练习赛
qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)