当前位置:网站首页>【js小知识】轻松了解js防抖与节流
【js小知识】轻松了解js防抖与节流
2022-06-12 05:29:00 【嘻咦啊写】
js的防抖和节流是前端面试经常会问到的知识点,今天结合moba类游戏来解释一下。
1.防抖:
持续触发不执行,不触发的一段时间后执行。就是指触发事件后在 n 秒内函数只能执行一次,如果在 n 秒内又触发了事件,则会重新计算函数执行时间。
就像王者中的回城一样,每次回城都需要一段时间的读条,读完条之后才执行回城这个操作。若在读条的时候再次触发回城,那么读条会被打断并且重新开始读条。这就是防抖。
实际应用:
//如下,持续触发 scroll 事件时,并不执行 handle 函数,当 1000 毫秒内没有触发 scroll 事件时,才会延时触发 scroll 事件
function debounce(fn, wait) {
var timeout = null;
return function() {
if(timeout !== null) clearTimeout(timeout);
timeout = setTimeout(fn, wait);
}
} // 处理函数
function handle() {
console.log(Math.random());
}
// 滚动事件 window.addEventListener('scroll', debounce(handle, 1000));
2.节流:
持续触发也执行,只是执行频率变低了。 就是指连续触发事件但是在 n 秒中只执行一次函数。节流会稀释函数的执行频率。
就像王者中的平A一样,你平A的多少只跟你的攻速有关系,跟你手速快慢没关系,即使你的手速比攻速还快,你的平A的多少也只和攻速有关。这就是节流。
实际应用:
//如下,持续触发 scroll 事件时,并不立即执行 handle 函数,每隔 1000 毫秒才会执行一次handle 函数
var throttle =function(func, delay) {
var prev = Date.now();
return function() {
var context = this;
var args = arguments;
var now = Date.now();
if (now - prev >= delay) {
func.apply(context, args);
prev = Date.now();
}
}
}
function handle() {
console.log(Math.random());}
window.addEventListener('scroll', throttle(handle, 1000));
3.共同点
就是限制函数执行次数,以达到提高用户体验目的。
边栏推荐
- WiFi protocol and ieee905 protocol learning details
- Introduction to audio alsa architecture
- Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
- Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
- 利用jieba库进行词频统计
- 38. arrangement of strings
- Pupanvr- some tool class objects (6)
- Platform of ASoC framework driven by alsa
- The way to promote software test engineer
- Nature | 给全球的新冠伤亡算一笔账
猜你喜欢
![[cjson] precautions for root node](/img/6c/4462bc8126935f00a5c64f18de13ea.png)
[cjson] precautions for root node

Enhanced vegetation index evi, NDVI data, NPP data, GPP data, land use data, vegetation type data, rainfall data

WiFi protocol and ieee905 protocol learning details

Matlab: image rotation and interpolation and comparison of MSE before and after

公司注册认缴资金多久

Performance test - GTI application service performance monitoring platform

Classes and objects, methods and encapsulation

Project requirements specification

Test work summary - performance test indicators

个体工商户是不是法人企业
随机推荐
Automated test - dark horse headline test project
Selenium crawler automatically captures TOEFL test position of NEEA website
Introduction to audio alsa architecture
17. print from 1 to the maximum n digits
Index fund summary
How to count the total length of roads in the region and draw data histogram
43. Number of occurrences of 1 in 1 ~ n integers
Pupanvr create project and management script (2)
Matlab: image rotation and interpolation and comparison of MSE before and after
Detailed usage of vim editor
JS set the position of the current scroll bar
Pupanvr- an open source embedded NVR system (1)
Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution
Multi thread learning III. classification of threads
JS to determine whether it is the first time to browse the web page
Main business objects of pupanvr record (5)
Nature | make an account of the new crown casualties in the world
59 - II. Maximum value of the queue
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e
Fundamentals of intensive learning openai gym environment construction demo