当前位置:网站首页>【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.共同点
就是限制函数执行次数,以达到提高用户体验目的。
边栏推荐
- 60. points of N dice
- Pupanvr create project and management script (2)
- JS set the position of the current scroll bar
- How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)
- WebRTC AEC 流程解析
- Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering
- CODIS stress test (PHP)
- Lvgl8.1 hi3536c platform use
- Selenium crawler automatically captures TOEFL test position of NEEA website
- Detailed tutorial on the use of yolov5 and training your own dataset with yolov5
猜你喜欢

Yolo opencv scale identification scale reading identification water gauge identification water level identification source code

The most commonly used objective weighting method -- entropy weight method

Servlet core

CODIS long link test

Yolov5 realizes road crack detection

Performance test - GTI application service performance monitoring platform

利用jieba库进行词频统计

Vivado HLS introductory notes

38. appearance series

Servlet core technology
随机推荐
What is thinking
个体工商户是不是法人企业
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
Necessary for Test Engineer -- package capturing tool fiddler
Static keyword and inheritance, polymorphic and special classes
Yolov5 realizes road crack detection
Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
JS set the position of the current scroll bar
17. print from 1 to the maximum n digits
Reason: Canonical names should be kebab-case (‘-‘ separated), lowercase alpha-numeric characters and
29. print matrix clockwise
PHP实现图片登录验证码的解决方案
The server time zone value ‘Ö Ð¹ ú±ê ×¼ ʱ ¼ ä‘ is unrecognized or represents more than one time zone. You
Pupanvr create project and management script (2)
How long is the company's registered capital subscribed
Soil type, soil texture, soil nutrient and change data, soil organic matter, soil pH, soil nitrogen, phosphorus and potassium
Pupanvr- an open source embedded NVR system (1)
Classes and objects, methods and encapsulation
Google reinforcement learning framework seed RL environment deployment
How to count the total length of roads in the region and draw data histogram