Open In App

Lodash _.throttle() Method

Last Updated : 03 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The Lodash _.throttle() method limits a function’s execution to once every specified time interval, preventing it from being called too frequently. It’s ideal for performance optimization in events like scrolling or resizing, with built-in methods to cancel or immediately invoke delayed calls.

Moreover, it provides some options that 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!

Similar Reads