当前位置:网站首页>小程序的防抖节流怎么写?
小程序的防抖节流怎么写?
2022-06-28 02:59:00 【Dark_programmer】
1.概念
函数防抖(debounce)
函数防抖,就是指触发事件后在 n 秒内函数只能执行一次,如果在 n 秒内又触发了事件,则会重新计算函数执行时间。
简单的说,当一个动作连续触发,则只执行最后一次
。
如果有人进电梯(触发事件),那电梯将在10秒钟后出发(执行事件监听器),这时如果又有人进电梯了(在10秒内再次触发该事件),我们又得等10秒再出发(重新计时)
函数节流(throttle)
限制一个函数在一定时间内只能执行一次。
保证如果电梯第一个人进来后,10秒后准时运送一次
,这个时间从第一个人上电梯开始计时,不等待,如果没有人,则不运行
2. 代码封装
函数防抖(debounce)
/** * * @param {*} cb 回调函数 * @param {*} delay 延时时间 */
const debounce = (cb, delay = 1000) => {
let timer = null;
return function() {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
cb.apply(this, arguments);
}, delay);
};
};
函数节流(throttle)
/** * * @param {*} cb 回调函数 * @param {*} wait 等待时间 */
const throttle = (cb, delay = 1000) => {
//距离上一次的执行时间
let lastTime = 0;
return function() {
let _this = this;
let _arguments = arguments;
let now = new Date().getTime();
//如果距离上一次执行超过了delay才能再次执行
if (now - lastTime > delay) {
cb.apply(_this, _arguments);
lastTime = now;
}
};
};
3. 使用
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";
// 节流使用
clickHandle: throttle(function(...args) {
console.log("throttle", args);
console.log("throttle", this);
}, 2000),
// 防抖使用
inputHandle: debounce(function(...args) {
console.log("debounce args", args);
console.log("debounce this", this);
}, 1000),
边栏推荐
- 启牛开的证券账户是安全的吗?如何开账户呢
- Relative path writing of files
- 劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
- Idea auto generate code
- 多线程与高并发三:AQS底层源码分析及其实现类
- 2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units
- Lamaba表达式学习及常用函数式接口
- 新手开哪家的证券账户是比较好?股票开户安全吗
- No&nbsp; result&nbsp; defined&amp; nbsp…
- collections.defaultdict()的使用
猜你喜欢
劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
Solution to not displaying logcat logs during debugging of glory V8 real machine
Redis cluster setup [simple]
Necessary software tools in embedded software development
调试利器 go-spew
Relative path writing of files
Online DDL implementation mechanism in InnoDB of database Series
Execution plan in MySQL of database Series
随机推荐
第九章 APP项目测试(3) 测试工具
Go 數據類型篇(四)之浮點型與複數類型
力扣每日一题-第29天-523.在区间范围统计奇数数目
Scalable storage system (I)
Simple implementation of cool GUI window based on WPF
WARN:&nbsp;SQL&nbsp;Error:&nbsp;…
大咖说·计算讲谈社|什么是东数西算要解决的核心问题?
Li Kou daily question - day 29 -523 Count odd numbers in interval range
Set drop-down options on Excel files
Anaconda command usage
Online DDL implementation mechanism in InnoDB of database Series
TypeError:&nbsp;&#039;module&amp;#03…
How to automatically add author, time, etc. to eclipse
Win10 如何删除系统盘大文件hiberfil.sys
GAMES104 作业2-ColorGrading
根据Explain查看sql执行计划,对SQL进行优化
新手开哪家的证券账户是比较好?股票开户安全吗
__getitem__和__setitem__
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
多线程与高并发五:等待队列及Executor和线程池详解(重点)