当前位置:网站首页>lodash实现防抖和节流功能及原生实现
lodash实现防抖和节流功能及原生实现
2022-06-27 22:43:00 【学而时习之.】
这里以搜索功能为例
监听边输入边搜索:@input
敲回车搜索 @keyup.enter.native=""
点击按钮搜索 @click
搜索性能优化:主要利用防抖和节流
防抖和节流:主要目的是为了降低高频事件触发,减少dom操作或请求次数,提升性能
通常高频事件:onscroll,onresize,keyup/keydown,mousemove
防抖:在固定时间内,如果有事件触发,则会再延长固定时间,直到固定时间内没有触发事件再做处理 例如:电梯和屏保。
节流:指定一个固定时间,无论是事件触发与,只要到了固定时间,都会触发。
第三方函数工具库:lodash
安装: npm i lodash
引入:import _ from ‘lodash’
//防抖
getList:_.debounce(function() {
this.getUserList()
},2000),
//节流
getList:_.throttle(function() {
this.getUserList()
},2000),
js 原生(可写全局方法):
export function _debounce(fn, delay) {
//防抖
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);
};
}
// 节流
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);
}
}
}
边栏推荐
- Arduino UNO通过电容的直接检测实现简易触摸开关
- 投资场内ETF基金是靠谱吗,场内ETF基金安全吗
- 深入解析kubernetes controller-runtime
- #795 Div.2 E. Number of Groups set *
- 攻击队攻击方式复盘总结
- The development of the Internet provides new solutions for industrial transformation
- Summary of wuenda's machine learning course (14)_ Dimensionality reduction
- 认识微信小程序项目的基本组成结构
- 796 div.2 C. managing history thinking
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
猜你喜欢
Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
zotero文献管理工具安装使用
MATLB|基于复杂网络的配电系统微电网优化配置
剑指 Offer 61. 扑克牌中的顺子
翻译(5): 技术债务墻:一种让技术债务可见并可协商的方法
Promise是什么
炼金术(7): 何以解忧,唯有重构
What is promise
Latest MySQL advanced SQL statement Encyclopedia
Matlb| improved forward push back method for solving power flow of low voltage distribution network
随机推荐
内网IP和公网IP的区别及作用
数据仓库入门介绍
Mysql database tourism management system_ Jsp+mysql tourism management system based on SSM [easy to understand]
Promise是什么
【无标题】
MySQL十种锁,一篇文章带你全解析
Technical debt wall: a way to make technical debt visible and negotiable
Is it reliable to invest in exchange traded ETF funds? Is it safe to invest in exchange traded ETF funds
Which securities speculation account opening commission is the cheapest and safest
RNA-seq入门实战(一):上游数据下载、格式转化和质控清洗
Arduino UNO通过电容的直接检测实现简易触摸开关
Modern programming languages: zig
现代编程语言:zig
Matlb| improved forward push back method for solving power flow of low voltage distribution network
[black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation
Cloud native O & M article plan
Taro--- day1--- construction project
SCU|通过深度强化学习进行微型游泳机器人的步态切换和目标导航
[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science
现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)