当前位置:网站首页>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.
边栏推荐
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (5) Task Book
- TCP拥塞控制技术 与BBR的加速原理
- NLP自然语言处理(一)
- LoadBalancer load balancing
- WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template
- Stimulsoft ReportsJS and DashboardsJS. 2022.3.3
- 小程序毕设作品之微信二手交易小程序毕业设计成品(3)后台功能
- The curl command to get the network IP
- Tcp编程
- Monitor page deployment
猜你喜欢

Public chains challenging the "Impossible Triangle"

Software testing interview questions and answer analysis, the strongest version in 2022

route filter

sqlmap使用教程大全命令大全(图文)

Nacos服务注册与发现

Transformation of traditional projects

小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书

nSoftware.PowerShell.Server.2020

朴素贝叶斯分类

Gateway 路由网关
随机推荐
route filter
组织在线化:组织数字化变革的新趋势
OpenFeign realize load balance
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
小程序毕设作品之微信二手交易小程序毕业设计成品(6)开题答辩PPT
Is the snowflake the same question?
微服务进阶 Cloud Alibaba
淘宝/天猫获得淘宝店铺详情 API
Summary of Rpc and gRpc Introduction
nSoftware.PowerShell.Server.2020
朴素贝叶斯分类
flutter 记录学习不一样的动画(一)
MySQ deadlock
Nacos服务注册与发现
[Flink] How to determine the cluster planning size from development to production launch?
Organizations Going Online: A New Trend in Organizational Digital Transformation
OpenFeign实现降级
Open address method hash implementation - linear detection method
运行时间监控:如何确保网络设备运行时间
Hystrix service circuit breaker