A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-debounce-method/ below:

Lodash _.debounce() Method - GeeksforGeeks

Lodash _.debounce() Method

Last Updated : 09 Jan, 2025

The Lodash _.debounce() method is a utility function that delays the execution of a given function until a specified wait time has passed since the last invocation. This method is particularly useful in scenarios where events fire frequently, such as resizing, scrolling, or input events, helping prevent unnecessary or excessive function calls that can affect performance

How _.debounce() Works

_.debounce() creates a debounced version of a function, ensuring it is only called once after a certain wait period has elapsed since the last event. This helps manage rapid-fire events more efficiently, reducing the number of times the function is executed.

Lodash _.debounce() Method Syntax
_.debounce( func, wait, options{})
Parameters:

Return Value: Returns a new debounced function

Understanding how to optimize performance with techniques like debouncing is essential for modern development.

Examples of Lodash _.debounce() Method

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!!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4