当前位置:网站首页>Anti-shake and throttling
Anti-shake and throttling
2022-07-30 03:44:00 【mrcsunflower】
目录
一、防抖(debounce)
1.1 基本概念
间隔时间内,多次触发事件,以最后一次为准.
1.2 应用场景
键盘输入框(oninput)、鼠标移动(onmousemove)、调整浏览器窗口大小(resize)等.
1.3 防抖思路
- 声明一个变量存储定时器id
- 每一次触发事件的时候,don't trigger,开启定时器,Trigger after the interval
- 如果在间隔时间内,The user triggered other events,The last timer ends,以最后一次为准
- 开启本次定时器
1.4 eg
<input type="text">
<script>
let input = document.querySelector("input");
let time = null;
// oninput:当用户向 <input> 中尝试输入时执行 JavaScript:
input.oninput = function () {
if (time !== null) {
/**
* clearTimeout() 方法可取消由 setTimeout() 方法设置的定时操作.
clearTimeout() 方法的参数必须是由 setTimeout() 返回的 ID 值.
注意: 要使用 clearTimeout() 方法, 在创建执行定时操作时要使用全局变量:
*/
clearTimeout(time);
}
time = setTimeout(() => {
console.log(this.value);
}, 500)
}
</script>Use closures to encapsulate
<input type="text">
<script>
let input = document.querySelector("input");
input.oninput = debounce(function () {
console.log(this.value);
}, 500);
// Use closures to implement encapsulated anti-shake methods
function debounce(fn, delay) {
let time = null;
return function () {
if (time !== null) {
clearTimeout(time);
}
time = setTimeout(() => {
fn.call(this);
}, 500)
}
}
</script>二、节流(throttle)
2.1 基本概念
间隔时间内函数只被触发一次.
2.2 作用
Reduce high frequency event trigger frequency.
2.3 应用场景
高频事件:如鼠标移动(onmousemove)、鼠标滚动条(onscroll)等.
2.4 节流思路
- 声明变量存储本次触发的时间 time
- 每一次触发的时候,使用当前时间 - time,Determine whether the interval between two times exceeds the throttling time
- 超过:触发,并且存储本次触发时间
- 不超过:不触发
2.5 eg
body {
height: 2000px;
}<script>
let flag = true;
window.onscroll = function () {
if (flag) {
setTimeout(() => {
console.log("123");
flag = true;
}, 500);
}
flag = false;
}
</script>Use closures to encapsulate
<script>
window.onscroll = throttle(function () {
console.log("123");
}, 500);
// 封装
function throttle(fn, delay) {
let flag = true;
return function () {
if (flag) {
setTimeout(() => {
fn.call(this);
flag = true;
}, delay);
}
flag = false;
}
}
</script>三、相同点与不同点
相同点:All are to optimize the execution frequency of the function,提高网页性能.
不同点:
防抖:Optimize user-triggered events,Multiple triggers are subject to the last one.
节流:优化 ‘事件本身’ 执行的频率,Interval refers to one execution.
边栏推荐
- Nacos achieves high availability
- Nacos cluster partition
- Nacos installation and deployment
- Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report
- 淘宝/天猫获得淘宝店铺详情 API
- SDL player in action
- spicy (1) basic definition
- C# 一周入门之《C#-类和对象》Day Six
- Stimulsoft ReportsJS and DashboardsJS. 2022.3.3
- STM32 SPI+WM8978语音回环
猜你喜欢

Three years of experience will only be a little bit (functional testing), and you may not even be able to find a job after resigning.

Introduction to management for technical people 1: What is management

传统项目转型

WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template

Nacos Configuration Center

小程序毕设作品之微信积分商城小程序毕业设计成品(7)中期检查报告

MyCat中对分库分表、ER表、全局表、分片规则、全局序列等的实现与基本使用操作

一起来学习flutter 的布局组件

Chapter 51 - Knowing the request header parameter analysis【2022-07-28】

【Node访问MongoDB数据库】
随机推荐
WPF 学习笔记《WPF布局基础》
curl命令获取外网ip
组织在线化:组织数字化变革的新趋势
Overview of Federated Learning (1) - Background, Definition and Value of Federated Learning
Open address method hash implementation - linear detection method
The most important transport layer
数据湖:数据集成工具DataX
Organizations Going Online: A New Trend in Organizational Digital Transformation
The curl command to get the network IP
Answer these 3 interview questions correctly, and the salary will go up by 20K
What is the difference between mission, vision and values?
vscode 调试和远程
Nacos集群分区
历经5面的阿里实习面经篇~
MySQL之数据查询(分类汇总与排序)
OpenFeign realize load balance
[Flink] How to determine the cluster planning size from development to production launch?
NLP自然语言处理(一)
MyCat中对分库分表、ER表、全局表、分片规则、全局序列等的实现与基本使用操作
Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!