当前位置:网站首页>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')}边栏推荐
- WeChat chat record search in a red envelope
- R语言ggstatsplot包ggbarstats函数可视化条形图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)、检验结果报告符合APA标准
- 【MySQL】Mysql范式及外键作用
- org.apache.jasperException(could not initialize class org)
- 梅克尔工作室-第一次
- Matlab matrix basic operations (definition, operation)
- WPF project - basic usage of controls entry, you must know XAML
- How useful is four-quadrant time management?
- Groupid(artifact id)
- DBeaver连接MySQL 8.x时Public Key Retrieval is not allowed 错误解决
猜你喜欢

The use of border controls

TRACE32 - C source code association

Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut

leetcode303 Weekly Match Replay

模板与泛型编程值typelist实现

《微信小程序-进阶篇》Lin-ui组件库源码分析-Icon组件

思路迪医药冲刺港股:5个月亏2.9亿 泰格医药与先声药业是股东

TRACE32 - SNOOPer-based variable logging

Getting started with UnityShader (3) - Unity's Shader

Introductory UnityShader learning (2) - the rendering pipeline
随机推荐
微信聊天记录中搜索红包
button控件的使用
what exactly is json (c# json)
基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)
乡村基冲刺港交所:5个月期内亏2224万 SIG与红杉中国是股东
PDF 拆分/合并
为什么黑客领域几乎一片男生?
Groupid(artifact id)
Synchronized和volatile 面试简单汇总
Small test knife: Go reflection helped me convert Excel to Struct
自适应控制——仿真实验三 用超稳定性理论设计模型参考自适应系统
Getting started with UnityShader (3) - Unity's Shader
TRACE32——常用操作
Codeforces Round #796 (Div. 2)(A-D)
ASP.NET Core generates continuous Guid
自适应控制——仿真实验二 用Narendra方案设计模型参考自适应系统
华医网冲刺港股:5个月亏2976万 红杉与姚文彬是股东
数据表插入数据insert into
R language ggplot2 visualization: use the ggmapplot function of the ggpubr package to visualize the MA plot (MA-plot), the font.legend parameter and the font.main parameter to set the title and legend
AVH部署实践 (一) | 在Arm虚拟硬件上部署飞桨模型