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
In the process of team development, it is essenti...
1. System environment [root@localhost home]# cat ...
1. Download 1. Download the installation package ...
This article example shares the specific code of ...
If you set the table-layer:fixed style for a tabl...
Preface: Based on a recent medical mobile project...
Table of contents 1. Installation preparation 1. ...
Preface NAT forwarding: Simply put, NAT is the us...
Table of contents Preface Why How much is it? Num...
When using Navicat to connect to a remote Linux M...
In the development process of Vue project, for th...
Redis is a distributed cache service. Caching is ...
The hyperlink <a> tag represents a link poin...
Table of contents Ref and Reactive Ref Reactive T...
This article introduces how to build a high-avail...