Debouncing in JavaScript
Debouncing in JavaScript is a practice used to improve browser performance. There might be some functionality in a web page which requires time-consuming computations. If such a method is invoked frequently, it might greatly affect the performance of the browser, as JavaScript is a single threaded language. Debouncing is a programming practice used to ensure that time-consuming tasks do not fire so often, that it stalls the performance of the web page. In other words, it limits the rate at which a function gets invoked.
<html> <body> <button id= "debounce" > Debounce </button> <script> var button = document.getElementById( "debounce" ); const debounce = (func, delay) => { let debounceTimer return function () { const context = this const args = arguments clearTimeout(debounceTimer) debounceTimer = setTimeout(() => func.apply(context, args), delay) } } button.addEventListener( 'click' , debounce( function () { alert( "Hello\nNo matter how many times you" + "click the debounce button, I get " + "executed once every 3 seconds!!" ) }, 3000)); </script> </body> </html> |
Output: Alertbox after 3 seconds
Hello No matter how many times you click the debounce button, I get executed once every 3 seconds!!
Explanation:
The button is attached to an event listener that calls the debounce function. The debounce function is provided 2 parameters – a function and a Number.
Declared a variable debounceTimer, which as the name suggests, is used to actually call the function, received as a parameter after an interval of ‘delay’ milliseconds.
If the debounce button is clicked only once, the debounce function gets called after the delay. However, if the debounce button is clicked once, and again clicked prior to the end of the delay, the initial delay is cleared and a fresh delay timer is started. The clearTimeout function is being used to achieve it.
The general idea for debouncing is:
1. Start with 0 timeout
2. If the debounced function is called again, reset the timer to the specified delay
3. In case of timeout, call the debounced function
Thus every call to a debounce function, resets the timer and delays the call.
Application:
Debouncing can be applied in implementing suggestive text, where we wait for the user to stop typing for a few seconds before suggesting the text. thus, on every keystroke, we wait for some seconds before giving out suggestions.
Another application of debouncing is in content-loading webpages like Facebook and Twitter where the user keeps on scrolling. In these scenarios, if the scroll event is fired too frequently, there might be a performance impact, as it contains lots of videos and images. Thus the scroll event must make use of debouncing.
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- Map.get( ) In JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : JayantaTalukdar