当前位置:网站首页>Implement anti-shake and throttling functions
Implement anti-shake and throttling functions
2022-07-31 15:20:00 【Mo Mingqi Miu Mi】
What is throttling
Specify a unit of time, within which the callback function that triggers the event can only be executed once
1. Timer
let box = document.querySelector( ".box")box.addEventListener( "touchmove", throttle(demo, 2000))function throttle(event, time){let timer = nullreturn function(){if(!timer) {timer = setTimeout(() =>{event();timer = null;}, time);}}}function demo(){console.log('Initiate request')}
2. Timestamp
var throttle = function(fn, delay) {var prev = Date.now();return function() {var context = this;var args = arguments;var now = Date.now();if (now - prev >= delay) {fn.apply(context, args);prev = Date.now();} }} }}function handle() {console.log(Math.random());}window.addEventListener('scroll', throttle(handle, 1000));
What is anti-shake
When the event is triggered continuously, the event handler will only be executed once if the event is not triggered again within a certain period of time. If the event is triggered again before the set time arrives, the delay will start again.
let telInput = document.querySelector( 'input' )telInput.addEventListener( 'input' , antiShake(demo, 2000))//Anti-shake packagefunction antiShake(fn, wait){let timeOut = null;return args =>{if(timeout) clearTimeout(timeOut)timeOut = setTimeout(fn, wait);}}function demo(){console.log('Initiate request')}
边栏推荐
- 基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)
- [MySQL] Mysql paradigm and the role of foreign keys
- Groupid(artifact id)
- 乡村基冲刺港交所:5个月期内亏2224万 SIG与红杉中国是股东
- 使用 GraphiQL 可视化 GraphQL 架构
- Gorm—Go language database framework
- The normal form of the database (first normal form, second normal form, third normal form, BCNF normal form) "recommended collection"
- 7. Summary of common interview questions
- 【MySQL】Mysql范式及外键作用
- 工程流体力学复习
猜你喜欢
随机推荐
三、数组
WPF project - basic usage of controls entry, you must know XAML
The normal form of the database (first normal form, second normal form, third normal form, BCNF normal form) "recommended collection"
Doing things software development - the importance of law and understanding of reasonable conclusions
How useful is four-quadrant time management?
微信聊天记录中搜索红包
01 邂逅typescript,环境搭建
使用 Chainlink Keepers 实现智能合约函数的自动化执行
《微信小程序-进阶篇》Lin-ui组件库源码分析-Icon组件
Nuget package and upload tutorial
使用 PyTorch 检测眼部疾病
Word table to Excel
基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)
Kubernetes原理剖析与实战应用手册,太全了
TRACE32 - SNOOPer-based variable logging
Web自动化实战——Selenium4(自动化测试环境的搭建)
浏览器自带的拾色器
Destruction order of thread_local variables
Internet banking stolen?This article tells you how to use online banking safely
学习笔记12--路径-速度分解法之局部路径搜索