当前位置:网站首页>How to write anti shake throttling for small programs?
How to write anti shake throttling for small programs?
2022-06-28 03:49:00 【Dark_ programmer】
Anti shake throttling
1. Concept
Function anti shake (debounce)
Function anti shake , It means after triggering the event n Function can only be executed once per second , If in n Seconds to trigger the event , The function execution time is recalculated .
To put it simply , When an action is triggered continuously , Only the last time .
If someone enters the elevator ( Triggering event ), The elevator will be in 10 Seconds later ( Execute event listener ), At this time, if someone enters the elevator again ( stay 10 Trigger the event again within seconds ), We have to wait again 10 Seconds to go ( Retime )
Function throttling (throttle)
Restrict a function to execute only once in a certain time .
Make sure that if the first person in the elevator comes in ,10 Deliver it on time in seconds , This time starts from the first person on the elevator , Don't wait for , If no one , Does not run
2. Code encapsulation
Function anti shake (debounce)
/** * * @param {*} cb Callback function * @param {*} delay Delay Time */
const debounce = (cb, delay = 1000) => {
let timer = null;
return function() {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
cb.apply(this, arguments);
}, delay);
};
};
Function throttling (throttle)
/** * * @param {*} cb Callback function * @param {*} wait Waiting time */
const throttle = (cb, delay = 1000) => {
// Last execution time
let lastTime = 0;
return function() {
let _this = this;
let _arguments = arguments;
let now = new Date().getTime();
// If the distance from the last execution exceeds delay To execute... Again
if (now - lastTime > delay) {
cb.apply(_this, _arguments);
lastTime = now;
}
};
};
3. Use
mine.axml
<view class="root_container">
<input placeholder="Input" onInput="inputHandle" />
<button size="default" type="primary" catchTap='clickHandle'>Button</button>
</view>
mine.js
import {
debounce, throttle } from "/utils/index.js";
// Throttling use
clickHandle: throttle(function(...args) {
console.log("throttle", args);
console.log("throttle", this);
}, 2000),
// Anti shake use
inputHandle: debounce(function(...args) {
console.log("debounce args", args);
console.log("debounce this", this);
}, 1000),
边栏推荐
- How to modify a se38 editor theme
- uni-app 如何根据环境自动切换请求的地址
- Resource management, high availability and automation (Part 2)
- 解析STEAM教育框架下未来教师研究能力
- A pit filling trip based on LNMP to build a personal website
- What is the difference between slice and array in go question bank 12?
- Is it better for a novice to open a securities account? Is it safe to open a stock trading account
- Hot! Yolov6's fast and accurate target detection framework is open source (with source code download)
- nn. Parameter and torch nn. Init series of functions to initialize model parameters
- 17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点
猜你喜欢

Execution plan in MySQL of database Series

使用信号分析器

Resource management, high availability and automation (medium)

collections. Use of defaultdict()

解析教育机器人的综合应用能力

Dataloader parameter collate_ Use of FN

「运维有小邓」监控文件及文件夹变更

leetcode:单调栈结构(进阶)

applicationContext.getBeansOfType 获取一个接口下所有实现类 执行方法或者获取实现类对象等 操作应用场景学习总结

17 `bs object Node name h3 Parent ` parents get parent node ancestor node
随机推荐
Web APIs DOM-事件基础丨黑马程序员
学习---有用的资源
Summary of SQL basic syntax for C #
Solution to not displaying logcat logs during debugging of glory V8 real machine
Anaconda command usage
database
可扩展数据库(下)
TypeError:&nbsp;&# 039; module&amp;# 03…
vscode中出现无法在只读编辑器中编辑
使用信号分析器
组件拆分实战
如何修改SE38编辑器主题
Lost connection repair: make "hide and seek" nowhere to hide
How to modify a se38 editor theme
Li Kou daily question - day 29 -219 Duplicate Element II exists
Floating point and complex type of go data type (4)
继承
第14章 AC-DC电源前级电路 笔记一
小程序image组件不显示图片?
音频 scipy 中 spectrogram 的运作机制