Saturday, December 28, 2024
Google search engine
HomeLanguagesLodash _.debounce() Method

Lodash _.debounce() Method

Lodash _.debounce() method is used to create a debounced function that delays the given function until after the stated wait time in milliseconds has passed since the last time this debounced function was called.

The debounced function has a cancel method that can be used to cancel the function calls that are delayed and a flush method that is used to immediately call the delayed function. It also provides some options that can be used to imply whether the function stated should be called on the leading and/or the trailing edge of the wait timeout.

Note:

  • The function is called with the last arguments that are given to the debounced function. However, consequent calls to the debounced function return the result of the last function call.
  • When the leading and the trailing options are true, then the function will be executed immediately when it is the first call and the second call will be after the completion of the ‘wait’ time.
  • When the wait time is 0 and the leading option is false, then the func call is deferred until the next tick.

Syntax:

_.debounce( func, wait, options{})

Parameters:

  • 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 behavior of the method. It is an optional parameter
    • leading (boolean): If it is true the function will get executed immediately rather than waiting for the ‘wait’ time. The default value is false means it waits until the ‘wait’ time is not completed.
    • maxWait (number): It is the maximum number of times, the function will be called after completion of this time.
    • trailing (boolean): It defines the calling of a function to the specified time(wait time). by default, it sets to true.

Return Value:

This method returns the new debounced function.

Example 1: In this example, the function will be called after 1000ms as mentioned in the lodash.debounce() function.

Javascript




// Requiring lodash library
const lodash = require('lodash');
 
// Using lodash.debounce() method
// with its parameters
let debounce_fun = lodash.debounce(function () {
    console.log('Function debounced after 1000ms!');
}, 1000);
 
debounce_fun();


Output:

Function debounced after 1000ms!

Example 2: In this example, both optional parameters are true that’s why function is executing immediately without following the specified time.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Using _.debounce() method
// with its parameters
let debounced_fun = _.debounce(function () {
    console.log("function is executing immideately!!")
}, 5000, { leading: true, trailing: true });
debounced_fun();


Output:

function is executing immideately!!

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments