当前位置:网站首页>节流的两种写法-最近看到的这种写法
节流的两种写法-最近看到的这种写法
2022-06-29 06:38:00 【你还差的远呢。】
第一种
function throttle(fn, delay) {
let timer = 0
return function (fn, delay) {
if (timer) {
return false
}
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = 0
}, delay)
}
}
第二种
function throttle(fn, delay) {
let flag = true
return function (fn, delay) {
if (!flag) {
return false
}
flag = false
setTimeout(() => {
fn.apply(this, arguments)
flag = true
}, delay)
}
}
总结: 两种写法,实际上效果是一种的,第一种写法代码上更简洁一些。只是 if 里面的判断变了而已
边栏推荐
猜你喜欢

Testing grpc service with grpcui

Redis in NoSQL database (4): redis publishing and subscription

Qt QLineEdit详解

Ci tool Jenkins II: build a simple CI project

Suggestions on digital transformation of large chemical enterprises

YGG cooperated with Web3 platform leader to empower the creative community with Dao tools and resources

Differences between JSON objects and JSON strings

利用IPv6实现公网访问远程桌面

Qt 串口编程

Tree drop-down selection box El select combined with El tree effect demo (sorting)
随机推荐
Some thoughts on port forwarding program
Tree drop-down selection box El select combined with El tree effect demo (sorting)
Daily question 1 - force deduction - there are three consecutive arrays of odd numbers
Utilisation d'IPv6 pour réaliser l'accès public au bureau distant
[answer all questions] CSDN question and answer function evaluation
Draw multiple ROC curves on a graph
期末总结——Spark
Configuring MySQL 5.7 and 8 under CentOS
Multimodal learning pooling with context gating for video classification
LeetCode_ Dynamic programming_ Medium_ 91. decoding method
数字ic设计——UART
VerilogA - dynamic comparator
转:侯宏:企业数字化转型的关键不是技术,而是战略
In vscade, how to use eslint to lint and format
Introduction to QT qfileinfo
UVM authentication platform
Error: GPG check FAILED Once install MySQL
Markdown 技能树(4):链接
QT serial port programming
Chinese garbled code on idea console [valid through personal test]