Lodash _.debounce() Method

23 Sep, 2020
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.debounce() method of Function in lodash is used to create a debounced function which delays the given func until after the stated wait time in milliseconds have passed since the last time this debounced function was called. The debounced function has a cancel method that can be used to cancel the func calls that are delayed and a flush method which is used to immediately call the delayed func. 
It also provides some options that can be used to imply whether the func stated should be called on the leading and/or the trailing edge of the wait timeout.
Note:
The func is called with the last arguments that are given to the debounced function. However, consequent calls to the debounced function returns the result of the last func call.
When the leading and the trailing options are true, then func is called on the trailing edge of the timeout if and only if the debounced function is called more than once throughout the wait timeout.
When the wait time is 0 and the leading option is false, then the func call is deferred until to the next tick.
Syntax:
_.debounce( func, wait, options )

Parameters: This method accepts three parameters as mentioned above and described below:
func: It is the function that has to be debounced.
wait: It is the number of milliseconds for which the calls are to be delayed. It is an optional parameter. The default value is 0.
options: It is the options object that can be used for changing the behaviour of the method. It is an optional parameter.
The options object has the following parameters:
leading: It defines the calling on the leading edge of the timeout. It is an optional parameter. The default value is false.
maxWait: It is the maximum number of time for which the func is allowed to be delayed before it is called. It is an optional parameter.
trailing: It defines the calling on the trailing edge of the timeout. It is an optional parameter. The default value is true.
Return Value: This method returns the new debounced function.
Example 1: In this example, one can enter the REPL again in 1000m after the function call as the waiting time is 1000ms.
Javascript
// Requiring lodash libraryconst _ = require('lodash');  // Using _.debounce() method// with its parametersvar debounce_fun = _.debounce(function () {  console.log('Function debounced after 1000ms!');  }, 1000);  debounce_fun();
Output:
Function debounced after 1000ms!

Example 2: In this example, the loop does not stop until it is stopped manually.
Javascript
// Requiring lodash libraryconst _ = require('lodash');  // Using _.debounce() method// with its parametersvar debounce_fun = _.debounce(function() {  console.log('Function debounced after 1000ms!');  }, 4, 1000, {'leading': false});  // Defining loopvar loop = function() {    setTimeout(loop, 3)    debounce_fun();};  // Calling loop to startloop();
Output:
Function debounced after 1000ms!
Function debounced after 1000ms!
Function debounced after 1000ms!
Function debounced after 1000ms!
Function debounced after 1000ms!
Function debounced after 1000ms!
Function debounced after 1000ms!
Function debounced after 1000ms!
.
.
.
.
// Will go on unless stopped manually

JavaScript-Lodash
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Hide or show elements in HTML using display property
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?