Preface1. Debounce: After a high-frequency event is triggered, the function will only be executed once within n seconds. If the high-frequency event is triggered again within n seconds, the time will be recalculated. For example: Just like when searching on Baidu, there will be associated words popping up after each input. This method of controlling the associated words cannot be triggered as soon as the content of the input box changes. It must be triggered after a period of time after you finish inputting. Throttling: High-frequency events are triggered, but they are only executed once within n seconds, so throttling will dilute the execution frequency of the function For example: a function is scheduled to be executed only when it is greater than or equal to the execution cycle, and it will not be executed if called during the cycle. It’s like when you’re trying to buy a limited-edition hot-selling item on Taobao, you keep clicking on the refresh button to buy, but there comes a time when no clicks work. This is where throttling comes in, because you’re afraid that clicking too quickly will cause a bug in the system. 2. Difference: Anti-shake is to turn multiple executions into the last execution, and throttling is to turn multiple executions into executions at intervals. Function anti-shake and throttling are always interview topics. Everyone may be familiar with the writing of function anti-shake and throttling, but there is a small episode when using function anti-shake or throttling in Vue. Correct usage posture in Vue Why? This is related to the event binding principle of Vue, which will not be introduced in detail here. If used directly inside the function body, the final result is that an anonymous immediately executed function is executed, which is wrong. Detailed reference principleFunction deshaking
Code Writing First non-immediate execution export function debounce(f, t){ let timer; return (...arg) => { clearTimeout(timer); timer = setTimeout(() => { f( ...arg) }, t) } } First immediate execution
export function debounceFirstExe(f, t){ let timer, flag = true; return (...args) => { if (flag){ f(...args); flag = false; }else { clearTimeout(timer); timer = setTimeout(() => { f(...args); flag = true; }, t) } } } Merged version export function debounce(f, t, im = false){ let timer, flag = true; return (...args) => { // Need to execute immediately if (im){ if (flag){ f(...args); flag = false; }else { clearTimeout(timer); timer = setTimeout(() => { f(...args); flag = true }, t) } }else { // Non-immediate execution clearTimeout(timer); timer = setTimeout(() => { f(...args) }, t) } } }
Function throttling
First non-immediate execution export function throttle(f,t){ let timer = true; return (...arg)=>{ if(!timer){ return; } timer=false; setTimeout(()=>{ f(...arg); timer=true; },t) } }
First immediate execution version export function throttleFirstExt(f, t) { let flag = true; return (...args) => { if (flag) { f(...args); flag = false; setTimeout(() => { flag = true }, t) } } }
Merged version export function throttle(f, t, im = false){ let flag = true; return (...args)=>{ if(flag){ flag = false im && f(...args) setTimeout(() => { !im && f(...args) flag = true },t) } } } Application scenarios:
SummarizeThis is the end of this article about the correct use of Vue function anti-shake and throttling. For more relevant Vue function anti-shake and throttling content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to quickly build ELK based on Docker
>>: Solution to the problem that MySql always pops up the mySqlInstallerConsole window
When installing Tomcat in Docker, Tomcat may over...
I hope to align the title on the left and the dat...
Table of contents origin Environmental Informatio...
Stored Procedures 1. Create a stored procedure an...
Flex(彈性布局) in CSS can flexibly control the layout...
How to use if in Linux to determine whether a dir...
The Golden Rule No matter how many people are wor...
Table of contents 1. Front-end routing implementa...
Table of contents 1. context 1. Usage scenarios 2...
This article shares the installation tutorial of ...
Table of contents 1. Animated Christmas Tree Made...
Preface The role of process management: Determine...
Table of contents The background is: What will ha...
MySQL has non-standard data types such as float a...
Why optimize: With the launch of the actual proje...