当前位置:网站首页>Lodash realizes anti shake and throttling functions and native implementation
Lodash realizes anti shake and throttling functions and native implementation
2022-06-28 01:02:00 【Learn from time to time】
Take the search function as an example
Listen, input and search :@input
Hit enter to search @keyup.enter.native=""
Click the button to search @click
Search performance optimization : Anti shake and throttling are mainly used
Anti shake and throttling : The main purpose is to reduce the triggering of high-frequency events , Reduce dom Number of operations or requests , Lifting performance
Usually high frequency events :onscroll,onresize,keyup/keydown,mousemove
Shake proof : In a fixed time , If an event triggers , Then the fixed time will be extended , Do not process until no event is triggered within a fixed time for example : Elevator and screen saver .
throttle : Specify a fixed time , Whether it's an event trigger or , As long as the fixed time comes , Will trigger .
Third party function tool library :lodash
install : npm i lodash
introduce :import _ from ‘lodash’
// Shake proof
getList:_.debounce(function() {
this.getUserList()
},2000),
// throttle
getList:_.throttle(function() {
this.getUserList()
},2000),
js Native ( Writable global methods ):
export function _debounce(fn, delay) {
// Shake proof
var delay = delay || 200;
var timer;
return function() {
var th = this;
var args = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function() {
timer = null;
fn.apply(th, args);
}, delay);
};
}
// throttle
export function _throttle(fn, interval) {
var last;
var timer;
var interval = interval || 200;
return function() {
var th = this;
var args = arguments;
var now = +new Date();
if (last && now - last < interval) {
clearTimeout(timer);
timer = setTimeout(function() {
last = now;
fn.apply(th, args);
}, interval);
} else {
last = now;
fn.apply(th, args);
}
}
}边栏推荐
- 供应链高效管理供应商
- Acwing第 57 场周赛【未完结】
- Cloud native O & M article plan
- Introduction to data warehouse
- Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
- Redis主从复制、哨兵模式、集群的概述与搭建
- MATLAB basic function length function
- 剑指 Offer 65. 不用加减乘除做加法
- Is it safe for Xiaobai in the stock market to open an account on the Internet?
- Translation (4): matching rules for automatic text completion
猜你喜欢

Matlb| improved forward push back method for solving power flow of low voltage distribution network

LabVIEW continuous sampling and limited sampling mode

电商转化率这么抽象,到底是个啥?

MySQL十种锁,一篇文章带你全解析

Overview and construction of redis master-slave replication, sentinel mode and cluster

What are cookies and the security risks of v-htm

electron窗口背景透明无边框(可用于启动页面)

Squid代理服务器(缓存加速之Web缓存层)

剑指 Offer 65. 不用加减乘除做加法

无人机专用滑环定制要求是什么
随机推荐
What is promise
给女朋友看的消息中间件
章凡:飞猪基于因果推断技术的广告投后归因
MATLB|基于复杂网络的配电系统微电网优化配置
Is the securities registration account safe? Is there any risk?
Download, configuration and installation of MySQL
From small to large, why do you always frown when talking about learning
#795 Div.2 D. Max GEQ Sum 单调栈
Cloud native O & M article plan
手机股票开户安全吗,买股票在哪开户?
Installation and use of Zotero document management tool
Collection de cas d'effets spéciaux en cliquant sur la souris de la page Web
Sword finger offer 61 Shunzi in playing cards
Software engineering job design (1): [personal project] implements a log view page
Technical debt wall: a way to make technical debt visible and negotiable
炼金术(4): 程序员的心智模型
炼金术(9): 简约而不简单,永不停歇的测试 -- always_run
Matlb| optimal configuration of microgrid in distribution system based on complex network
golang 猴子吃桃子,求第一天桃子的数量
Message Oriented Middleware for girlfriends