当前位置:网站首页>防抖&节流 加强版
防抖&节流 加强版
2022-06-22 20:51:00 【前端粉刷匠】
节流
在定时器的时间范围内再次触发,则不执行,等当前定时器完成,才能启动下一个定时器任务。
// setTimeout方法,
function throttle1(fn, interval) {
let flag = true;
return function (...args) {
let that = this;
if (!flag) return;
flag = false;
setTimeout(() => {
fn.apply(that, args)
flag = true;
}, interval);
}
}
// 计算时间间隔法
function throttle2(fn, interval) {
let last = 0;
return function (...args) {
let that = this;
let now = +new Date();
if (now - last < interval) return;
last = now;
fn.apply(that, args)
}
}
防抖
每次事件触发则删除原来的定时器,建立新的定时器,如果你反复触发,则只会执行最后一次的定时器。
非立即执行版本
function debounce(fn, delay) {
let timer = null;
return function (...args) {
let that = this;
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(that, args)
}, delay);
}
}
// 立即执行版本(点击的时候会立即调用一次,如果在delay秒内连续触发不会再次调用,如果在delay秒内没有触发,下次点击的时候会再次触发)
function debounce(fn, delay) {
let timer = null;
return function () {
let that = this;
if (timer) clearTimeout(timer);
let callNow = !timer;
timer = setTimeout(() => {
timer = null;
}, delay);
if (callNow) fn.apply(that);
}
}
// 最终合成版
/** * @desc 函数防抖 * @param func 目标函数 * @param wait 延迟执行毫秒数 * @param immediate true - 立即执行, false - 延迟执行 */
function debounce(func, delay, immediate) {
let timer;
return function() {
let that = this,
args = arguments;
if (timer) clearTimeout(timer);
if (immediate) {
let callNow = !timer;
timer = setTimeout(() => {
timer = null;
}, delay);
if (callNow) func.apply(that, args);
} else {
timer = setTimeout(() => {
func.apply
}, delay)
}
}
}
边栏推荐
- Codeup longest palindrome substring
- Introduction to database access tools
- Half optimized SQL
- Practice brings true knowledge: the strongest seckill system architecture in the whole network is decrypted. Not all seckills are seckills!!
- Why is yuancosmos so popular? Is the 10trillion yuan shouted by the market boasting or the truth?
- Dip1000,1 of D
- MySQL master-slave synchronization and its basic process of database and table division
- 2020-12-04
- Relationship between adau1452 development system interface and code data
- 2021-08-22
猜你喜欢

The xinjietu x70s has been listed for 87900 times and has leapfrogged the class in space safety. It is worthy of being a 7-seat SUV of the National University of China

2021-08-26

Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)

In the middle of the year, we will promote the integration of worry free, and the value-added package will be reduced by 6

SourceTree版本管理常用操作

5 minutes to quickly launch web applications and APIs (vercel)

LinkedList source code analysis

Reinforcement learning weekly (issue 50): saferl kit, gmi-drl, rp-sdrl & offline meta reinforcement learning

2021-04-16

保证数据库和缓存的一致性
随机推荐
Business stability construction ideas based on Cloud Architecture
Redis error reporting and common configurations
js图片分辨率压缩
Enabling partners, major guarantee of Spring Festival "non-stop"
Technology cloud report: East to West computing is not only about "computing", but also needs "new storage"
One case of SQL performance degradation caused by modifying implicit parameters
2021-05-02
Using the hbuilder x editor to install a solution for terminal window plug-ins that are not responding
安装typescript环境并开启VSCode自动监视编译ts文件为js文件
MySQL master-slave synchronization and its basic process of database and table division
c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试
【Kubernetes 系列】Kubernetes 概述
In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!
Mysql database DQL query operation
2021-04-14
Plan and change of continuous repair
Which securities company is the safest and best choice for stock trading account opening
2021-08-26
Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)
Total number of combinations [standard backtracking + backtracking techniques -- reducing stack depth]