当前位置:网站首页>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')}边栏推荐
- 华医网冲刺港股:5个月亏2976万 红杉与姚文彬是股东
- Deployment application life cycle and Pod health check
- OpenShift 4 - Customize RHACS security policies to prevent production clusters from using high-risk registry
- TRACE32 - Common Operations
- SIGABRT 报错时的注意事项和解决方法
- 网银被盗?这篇文章告诉你如何安全使用网银
- org.apache.jasperException(could not initialize class org)
- 如何进行需求分析评审
- Small test knife: Go reflection helped me convert Excel to Struct
- Introductory UnityShader learning (2) - the rendering pipeline
猜你喜欢
随机推荐
Internet banking stolen?This article tells you how to use online banking safely
Efficient use of RecyclerView Section 3
最小费用最大流问题详解
自适应控制——仿真实验二 用Narendra方案设计模型参考自适应系统
Nuget package and upload tutorial
LeetCode二叉树系列——222.完全二叉树的节点个数
mysql黑窗口~建库建表
RecyclerView高效使用第三节
Tencent Cloud Deployment----DevOps
01 Encounter typescript, build environment
json到底是什么(c# json)
SQL、HQL、JPQL 到底有什么区别
R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、font.legend参数和font.main参数设置标题和图例字体加粗
工程力学复习资料
三、数组
Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
01 邂逅typescript,环境搭建
为什么黑客领域几乎一片男生?
7、常见面试口语提问问题汇总
Deployment application life cycle and Pod health check








