1. Function debounce 1. What is image stabilization?
If no event is triggered within the specified time, the event processing function will be called; The following example shows this: /*Define anti-shake function* func: pass in a function that will be called when the event is no longer triggered continuously* delay: define how long it will take to execute the passed callback function* */ function debounce(func,delay) { let timer = null // used to save the timer return function (...args) { // If the timer exists, clear the timer and then reset the timer if(timer !== null) clearTimeout(timer) timer = setTimeout(func, delay) // If the delay is exceeded, the func here will be called to receive the event. If necessary, the this pointer of func can be modified. Since timer has a reference to the outside, it will not be destroyed.} } /*Event processing function*/ function testDeBounce(){ console.log('How many times did I execute it??') } // Receive the function returned by debounce const temp = debounce(testDeBounce(),1000) /*Bind event, test anti-shake function*/ window.addEventListener('scroll',()=>{ temp() }); // This will call the event handler at least once, and at most will not be executed more times than the following window.addEventListener('scroll', testDeBounce); // If written like this, the event handler will be called every time the page scrolls To summarize the ideas:
2. Function Throttling
Function throttling implementation methods: timer, timestamp, timer + timestamp; 2.1 Timer ImplementationIdeas:
/* * Define the timer throttling function * func: incoming event processing function * delay: the timer callback is invalid within the time specified by delay * */ function throttle(func,delay) { let timer = null const context = this return function(...args){ // If the timer does not exist if(!timer){ timer = setTimeout(()=>{ func.apply(context,args) // Consider the environment of the returned function call, so this is not used directly here timer = null // clear the timer after delay},delay) } } } function test() { console.log('Aaaaah!') } const temp = throttle(test,1000) document.querySelector('button').addEventListener('click',()=>{ temp() }) 2.2 Timestamp Implementationvar 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)); 2.3 Timestamp + Timer// Throttle code (timestamp + timer): var throttle = function(func, delay) { var timer = null; var startTime = Date.now(); return function() { var curTime = Date.now(); var remaining = delay - (curTime - startTime); var context = this; var args = arguments; clearTimeout(timer); if (remaining <= 0) { func.apply(context, args); startTime = Date.now(); } else { timer = setTimeout(func, remaining); } } } function handle() { console.log(Math.random()); } window.addEventListener('scroll', throttle(handle, 1000)); This is the end of this article about what is JavaScript anti-shake and throttling. For more information about JavaScript anti-shake and throttling, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Two common solutions to html text overflow display ellipsis characters
>>: Graphical explanation of the solutions for front-end processing of small icons
This article introduces several methods of implem...
1. CSS Miscellaneous Icons There are three ways t...
Table of contents 1. Trigger Solution 2. Partitio...
Ⅰ. Problem description: Use CSS to achieve 3D con...
Nginx uses regular expressions to automatically m...
Preface For file or directory permissions in Linu...
1. Differences between JSON.stringify() and JSON....
Table of contents Introduction Uses of closures C...
Table of contents 1. User created script 2. Word ...
For example: Copy code The code is as follows: <...
Sometimes, in order to facilitate the export and ...
Preface When testing, in order to test the projec...
What to do if you forget Windows Server 2008R2 So...
Copy code The code is as follows: <html> &l...
Today, we use uniapp to integrate Echarts to disp...