Thursday, December 26, 2024
Google search engine
HomeLanguagesJavascriptLodash _.throttle() Method

Lodash _.throttle() Method

Lodash _.throttle() method is used to create a throttled function that can only call the func parameter maximally once per every wait milliseconds. The throttled function here has a cancel method which is used to cancel func calls that are delayed and it also has a flush method which is used to immediately call that delayed func.

Moreover, it provides some options that are used to imply whether the function stated should be called on the leading and/or the trailing edge of the wait timeout.

Syntax:

_.throttle(func, [wait=0], [options={}])

Parameters:

  • func(Function) is the function to be throttled.
  • wait(number) is the number of milliseconds for which the calls are to be throttled.
  • options(Object) is the options object.
    • options.leading(boolean) defines the calling on the leading edge of the timeout.
    • options.trailing(boolean) defines the calling on the trailing edge of the timeout.

Return Value:

This method returns the new throttled function

Note:

  • Here, func is called with the last arguments that are given to the throttled function. However, consequent calls to the throttled function return the result of the last function call.
  • Here, if the leading and the trailing options are true, then func is called on the trailing edge of the timeout if and only if the throttled function is called more than once throughout the wait timeout.
  • Here, if the wait time is 0 and the leading option is false, then the function call is delayed until the next tick.

Example 1: In this example, after the function is throttled after 1000ms the waiting time here is 1000ms.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling throttle() method with its parameter
let throt_fun = _.throttle(function () {
    console.log('Function throttled after 1000ms!');
}, 1000);
 
throt_fun();


Output:

Function throttled after 1000ms!

Example 2: In this example, after the function is throttled after 1000ms the waiting time here is 1000ms. So, here the loop doesn’t stop until you stop it manually.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling throttle() method with its parameter
let throt_fun = _.throttle(function () {
    console.log('Function throttled after 1000ms!');
}, 1000);
 
// Defining loop
let loop = function () {
    setTimeout(loop, 5)
    throt_fun();
};
 
// Calling loop to start
loop();


Output:

Function throttled after 1000ms!
Function throttled after 1000ms!
Function throttled after 1000ms!
Function throttled after 1000ms!
Function throttled after 1000ms!
Function throttled after 1000ms!
.
.
.
.
// So on until you stop it manually.

Example 3: In this example, the function is called on the trailing edge of the timeout.

Javascript




// Requiring lodash library
const _ = require('lodash');
 
// Calling throttle() method with its parameter
let throt_fun = _.throttle(function () {
    console.log('Function is called on the'
        + ' trailing edge of the timeout '
        + 'and throttled after 2000ms!');
}, 2000, { 'trailing': true });
 
throt_fun();


Output:

Function is called on the trailing edge of the timeout and throttled after 2000ms!

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