当前位置:网站首页>【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);
};
}
边栏推荐
- 剑指Offer||:链表(简单)
- es数据导出csv文件
- Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
- ZYNQ_ IIC read / write m24m01 record board status
- Source code analysis of kubernetes' process of deleting pod
- Investment transaction and settlement of the fund
- 22/02/14 study notes
- Conversion between HJ integer and IP address
- HJ character count
- kubernetes集群命令行工具kubectl
猜你喜欢
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
Software design of power control board
Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
分析 NFT 项目的 5 个指标
asp. Net datalist to display product information and pictures
异或的应用。(提取出数字中最右侧的1,面试中经常用的到)
Uninstall and reinstall the latest version of MySQL database. The test is valid
剑指Offer||:链表(简单)
SOC clock configuration
Kubernetes theoretical basis
随机推荐
Generation and verification of JWT token
Makefile
HJ成绩排序
MMR rearrangement (similarity is calculated by editing distance and repeatability)
Spark 离线开发框架设计与实现
[ thanos源码分析系列 ]thanos query组件源码简析
HJ21 简单密码
Leetcode learning records
Dataset filling data, and the use of rows and columns
Flex layout
Is it safe for flush to open an account online
Soft test -- software designer -- database design of afternoon questions
HJ整数与IP地址间的转换
flex布局
Ice, protobuf, thrift -- Notes
[thanos source code analysis series]thanos query component source code analysis
数字藏品市场“三大套路”
大型项目中的Commit Message规范化控制实现
Open62541 import nodeset file directly
Section VI UART of zynq