当前位置:网站首页>【js】-【节流、防抖函数】
【js】-【节流、防抖函数】
2022-06-28 07:48:00 【有趣的学习】
1 函数节流throttle
throttle.js创建一个节流函数,在 wait 毫秒内最多执行 callback 一次
function throttle(fn, wait) {
# 定义开始时间
let start = 0;
# 返回结果是一个函数
return function (e) {
// 获取当前时间戳
let now = Date.now();
// 判断
if (now - start >= wait) {
// 修正this指向问题
fn.call(this, e);
// 修改开始时间
start = now
}
}
}
这种方法,可以保证第一次一定被触发,后面就是间隔指定时间触发一次
2 函数防抖debounce
function debounce(fn, time) {
# 定时器变量
var timeId = null;
// 返回一个函数
return function () {
# 清空定时器
clearTimeout(timeId);
# 重新启动定时器
timeId = setTimeout(() => {
fn.apply(this);
}, time);
};
}
边栏推荐
- Today's notes 22/1/7
- Static resource compression reduces bandwidth pressure and increases access speed
- The solution of "user account control to continue, please enter administrator user name and password" appears in win10 Professional Edition
- Section Xi. Axi of zynq_ Use of DMA
- A single node obtains the lock lock of the order number
- QT -- communication protocol
- Study notes 22/1/19 and 22/1/20
- HJ21 简单密码
- Investment transaction and settlement of the fund
- asp. Net registration page
猜你喜欢

Cloud native (to be updated)

asp. Net datalist when there are multiple data displays

Resizing node of rediscluster cluster cluster mode

8 figures | analyze Eureka's first synchronization registry

云原生:云计算技术再次升级 开启全面云开发时代

22/02/14 study notes

Open62541 import nodeset file directly

Leetcode learning records

Kubernetes deploys a secret pit where thanos ruler sends repeated alarms

Safety training is the greatest benefit for employees! 2022 induction safety training for new employees
随机推荐
SOC serial port configuration
Spark 离线开发框架设计与实现
QT -- communication protocol
7-1 understand everything
Is it safe for flush to open an account online
HJ字符串排序
"Three routines" of digital collection market
Rediscluster cluster mode capacity expansion node
linux下修改mysql用户名root
Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers
flutter 实现摇一摇功能
Cloud native (to be updated)
Cloud native: cloud computing technology is upgraded again to open an era of comprehensive cloud development
Soft exam -- software designer -- afternoon question data flow diagram DFD
Ice - resources
Resizing node of rediscluster cluster cluster mode
8 figures | analyze Eureka's first synchronization registry
Evaluation of inverse Polish expression < difficulty coefficient >
Helloword routine for ROS
Investment transaction and settlement of the fund