当前位置:网站首页>【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);
};
}
边栏推荐
猜你喜欢

推荐系统系列精讲(第五讲): 排序模型的调优实践

Redis implements distributed locks

卸载重装最新版mysql数据库亲测有效

No suspense about the No. 1 Internet company overtime table

Section 8: DMA of zynq

22/02/15 study notes

Section 5: zynq interrupt

腾讯下半年继续裁员,所有事业群至少缩减 10%,对此你怎么看?关注者

Open62541 import nodeset file directly

Alibaba cloud server creates snapshots and rolls back disks
随机推荐
Study notes 22/1/19 and 22/1/20
What is EC blower fan?
How to configure DDR3 of dm8148
分析 NFT 项目的 5 个指标
How to insert a single quotation mark into a table as a data type in Oracle pl/sql
Modifying MySQL port number under Linux
Redis one master multi slave cluster setup
HJ删除字符串中出现次数最少的字符
[thanos source code analysis series]thanos query component source code analysis
Real time database - Notes
es6箭头函数中return的用法
Is it safe for flush to open an account online
Es data export CSV file
Section 9: dual core startup of zynq
Cloud native: cloud computing technology is upgraded again to open an era of comprehensive cloud development
PLC -- Notes
异或的应用。(提取出数字中最右侧的1,面试中经常用的到)
Leetcode learning records
ACM笔记
Ambari (VI) -- ambari API use