Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Lodash _.debounce() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 library
const _ = require('lodash');
  
// Using _.debounce() method
// with its parameters
var 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 library
const _ = require('lodash');
  
// Using _.debounce() method
// with its parameters
var debounce_fun = _.debounce(function() {
  console.log('Function debounced after 1000ms!');
  }, 4, 1000, {'leading': false});
  
// Defining loop
var loop = function() {
    setTimeout(loop, 3)
    debounce_fun();
};
  
// Calling loop to start
loop();


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

My Personal Notes arrow_drop_up
Last Updated : 23 Sep, 2020
Like Article
Save Article
Similar Reads
Related Tutorials