当前位置:网站首页>Anti shake & throttling enhanced version
Anti shake & throttling enhanced version
2022-06-22 23:39:00 【Front end plasterer】
throttle
Trigger again within the time range of the timer , Do not perform , Wait for the current timer to finish , To start the next timer task .
// setTimeout Method ,
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);
}
}
// Calculation time interval method
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)
}
}
Shake proof
Delete the original timer every time the event is triggered , Set up a new timer , If you repeatedly trigger , Only the last timer will be executed .
Not an immediate version
function debounce(fn, delay) {
let timer = null;
return function (...args) {
let that = this;
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(that, args)
}, delay);
}
}
// Execute version now ( When clicked, it will be called immediately , If in delay Second continuous trigger will not call again , If in delay No trigger within seconds , It will trigger again the next time you click )
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);
}
}
// Final composite
/** * @desc Function anti shake * @param func Objective function * @param wait Delay execution in milliseconds * @param immediate true - Execute now , false - Delay the */
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)
}
}
}
边栏推荐
- Ensure database and cache consistency
- 2021-08-21
- OJ daily practice - Verifying substring
- Uniapp modifies array properties, and the view is not updated
- Common operations of sourcetree version management
- web缓存技术
- 14. 最长公共前缀
- Array and string offset access syntax with curly braces is no longer support
- three.js模拟驾驶游览艺术展厅---打造超级相机控制器
- Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction
猜你喜欢
![[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board](/img/3c/717b4a481bfff1d37e3c271f37a7d6.png)
[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board

Digital data depth | about software self-control, source code left, no code right

wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz

JSBridge

2021-05-02

Is it difficult to turn weak current into professional network worker? Huawei pre-sales engineers share their own experience

MySQL master-slave synchronization and its basic process of database and table division

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

After passing the hcip exam, I still failed to change my career. What do professional network workers value most

Asynchronous FIFO
随机推荐
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
Web Caching Technology
Array and string offset access syntax with curly braces is no longer support
[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
Spark SQL Start(2.4.3)
eslint 简单配置
1. class inheritance (point)
Longest word in output string
程序员接私活兼职选择
2021-05-02
【22暑期复建1】 Codeforces Round #791 (Div. 2)
2021-03-06
Use the find command
Considerations for using redisson to operate distributed queues
阻止别人使用浏览器调试
Customize multi-level list styles in word
14. 最长公共前缀
Spark RDD Programming Guide(2.4.3)
Phantomjs实用代码段(持续更新中……)
反向代理HAProxy