Lodash _.throttle() Method

23 Sep, 2020
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.throttle() method in lodash 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 which are used to imply whether the func stated should be called on the leading and/or the trailing edge of the wait timeout.
Syntax:
_.throttle(func, wait, options)
Parameters: The method accept three parameters as mentioned above and described below:
func: It is the function to be throttled.
wait: It is the number of milliseconds for which the calls are to be throttled.
options: It is the options object.options.leading: It defines the calling on the leading edge of the timeout.options.trailing: It defines the calling on the trailing edge of the timeout.
options.leading: It defines the calling on the leading edge of the timeout.
options.trailing: It 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 func 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 func call is delayed until to the next tick.
Example 1:
Javascript
// Requiring lodash libraryconst _ = require('lodash');  // Calling throttle() method with its parametervar throt_fun = _.throttle(function () {        console.log('Function throttled after 1000ms!');    }, 1000);  throt_fun();
Output: Here, after the function is throttled after 1000ms as the waiting time here is 1000ms.
Function throttled after 1000ms!
Example 2:
Javascript
// Requiring lodash libraryconst _ = require('lodash');  // Calling throttle() method with its parametervar throt_fun = _.throttle(function() {        console.log('Function throttled after 1000ms!');    }, 1000);  // Defining loopvar loop = function() {    setTimeout(loop, 5)    throt_fun();};  // Calling loop to startloop();
Output: So, here the loop doesn’t stop until you stop it manually.
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: Here, the function is called on the trailing edge of the timeout.
Javascript
// Requiring lodash libraryconst _ = require('lodash');  // Calling throttle() method with its parametervar 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!
Reference: https://lodash.com/docs/4.17.15#throttle
JavaScript-Lodash
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?