Lodash _.debounce() Method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();

chevron_right


Output:

Function debounced after 1000ms!

Example 2: In this example, the loop does not stop until it is stopped manually.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();

chevron_right


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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.