当前位置:网站首页>Anti shake and throttling
Anti shake and throttling
2022-07-29 00:29:00 【qiansli12138】
This knowledge point is suddenly used in today's work , Just record it .
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style type="text/css">
#box {
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box">
22232
</div>
<!-- Woman <input type="radio" name="2" id="clickRadio" value="" /> -->
<!-- Anti shake strategy (debounce) When the event is triggered , Delay n Seconds before the callback , If in this n The second event is triggered again , Then the time will be counted again . -->
<script type="text/javascript">
// Shake proof : Anti shake is when the event is triggered continuously , Ensure that only the last event handler is executed
function debounce (fn) {
var timer = null
return function (){
clearTimeout(timer) // Delete the previous timer every time you click It is equivalent to clearing the last event
timer = setTimeout(() => {
fn()
},1000)
}
}
function sayHi () {
console.log(" Triggered the jitter for the last time ")
}
let box = document.querySelector("#box")
box.addEventListener("click", debounce(sayHi))
// throttle : When the event is triggered continuously , Make sure to call the event handler only once in a certain period of time
function throttle (fn) {
var flag = true
return function () {
if (!flag) return // flag by false Don't go down
flag = false // Now flag Set to false
setTimeout(() => {
fn()
flag = true // When the user handler succeeds, it will flag The assignment is TRUE, You can click again
},1000)
}
}
function flagFunction (){
console.log("777")
}
//let box = document.querySelector("#box")
//box.addEventListener("click", throttle(flagFunction))
</script>
</body>
</html>summary : Shake proof : In unit time , Only execute the last click event ;
throttle : In unit time , Multiple click events , The next operation will not be executed until the execution is completed .
边栏推荐
- 【开发教程11】疯壳·开源蓝牙心率防水运动手环-整机功能代码讲解
- 12个MySQL慢查询的原因分析
- 15.模型评估和选择问题
- Flyway's quick start tutorial
- [small bug diary] Navicat failed to connect to MySQL | MySQL service disappeared | mysqld installation failed (this application cannot run on your computer)
- 【微服务~Nacos】Nacos服务提供者和服务消费者
- Locally connect to redis on Windows Server
- NPM run serve stuck at 40%
- Dynamic programming problem (6)
- 2022dasctfjuly empowerment competition (reappearance)
猜你喜欢

Visual full link log tracking

Introduction and solution of common security vulnerabilities in web system CSRF attack

How to solve the problems of MQ message loss, duplication and backlog?

Basic knowledge of PHP language (super detailed)

Laravel permission control

110道 MySQL面试题及答案 (持续更新)

动态规划问题(三)

CV target detection model sketch (2)

MySQL 分库分表及其平滑扩容方案

Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)
随机推荐
CV target detection model sketch (2)
Laravel8 middleware realizes simple permission control
动态规划问题(二)
【飞控开发基础教程8】疯壳·开源编队无人机-I2C(激光测距)
Oracle实例无法启动的问题如何解决
软考 --- 数据库(4)SQL语句
@Detailed explanation of the use of transactional annotation
PTA (daily question) 7-77 encryption
【esn】 学习回声状态网络
The difference between {} and ${}
Statistical analysis of time series
Everything you have learned will come in handy at some point in your life (turn)
Detailed explanation of the usage of exists in MySQL
@PostConstruct注解详解
vulnhub:Sar
Advanced area of attack and defense world web masters training www robots
16.偏差、方差、正则化、学习曲线对模型的影响
Locally connect to redis on Windows Server
[applet project development -- JD mall] uni app commodity classification page (first)
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r