当前位置:网站首页>节流的两种写法-最近看到的这种写法
节流的两种写法-最近看到的这种写法
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 里面的判断变了而已
边栏推荐
- 【翻译】簇拥而出。构建现代应用程序的设计方法
- QT serial port programming
- Draw multiple ROC curves on a graph
- Instanceklass "suggestions collection" of hotspot class model
- Multimodal learning pooling with context gating for video classification
- Message queue avoiding repeated refund by idempotent design and atomic lock
- VerilogA - dynamic comparator
- package. Are you familiar with all configuration items and their usage of JSON
- Qt foreach关键字
- Twitter launches the test of anti abuse tool "safe mode" and adds enabling prompt
猜你喜欢

NoSQL数据库之Redis(一):安装 & 简介

多模态 —— Learnable pooling with Context Gating for video classification

关于数据库,你应该知道的事情

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

消息队列之通过幂等设计和原子锁避免重复退款

The meaning and calculation method of receptive field

How to fix Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorli

NoSQL数据库之Redis(四):Redis新数据类型

Idea integrated code cloud

Redis of NoSQL database (II): introduction to redis configuration file
随机推荐
Mongostat performance analysis
try anbox (by quqi99)
As a qualified network worker, you must master DHCP snooping knowledge!
Markdown 技能树(1):MarkDown介绍
【软件测试】接口——基本测试流程
利用IPv6實現公網訪問遠程桌面
IDEA 集成 码云
VerilogA - dynamic comparator
施工企业选择智慧工地的有效方法
Draw multiple ROC curves on a graph
NoSQL数据库之Redis(二):Redis配置文件介绍
QT qlineedit details
大型化工企业数字化转型建议
Redis (V) of NoSQL database: redis_ Jedis_ test
Markdown 技能树(2):段落及强调
Redis de la base de données nosql (v): redis Jedis Tests
JDBC connects to the database and socket sends the client.
Daily question - force deduction - multiply the found value by 2
国内代码托管中心- 码云
Tree drop-down selection box El select combined with El tree effect demo (sorting)