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={}])Parametersfunc(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 functionNote: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! Comment More infoAdvertise with us Next Article Lodash _.after() Method N nidhi1352singh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Lodash Similar Reads Lodash _.after() Method Lodash _.after() method is opposite of Lodash _.before() method. This method creates a function that invokes function once itâs called n or more times.Syntax:_.after(n, func);Parameters: n: This parameter holds the number n, which defines the number of calls before func is invoked.func: This paramet 2 min read Lodash _.ary() Method Lodash _.ary() method is used to create a function that invokes the given function, up to n arguments, ignoring any additional arguments.Syntax:_.ary( func, n )Parameters: This method accepts two parameters as mentioned above and described below:func: This parameter holds the function which will cap 2 min read Lodash _.before() Method Lodash _.before() method is the opposite of the Lodash _.after() method. This method is used to create a function that invokes func with the binding and arguments of the created function, while itâs called less than n times.Syntax:_.before(n, func);Parameters: n: This parameter holds the number n, w 2 min read Lodash _.bind() Method Lodash _.bind() method is used to create a function that invokes the given function with the binding of thisArg and it is used to bind a function to an object. When the function is called, the value of this will be the object. The _.bind.placeholder value, which defaults to _ in monolithic builds, m 2 min read Lodash _.bindKey() Method Lodash _.bindKey() method of Function in lodash is used to create a function that calls the method at the object[key] along with the partials added to the arguments it accepts.Note:This method is different from the _.bind() method as it permits bound functions to mention methods that may be reinterp 3 min read Lodash _.curry() Method Lodash _.curry() method returns a curried version of the given function. it accepts an arity which is a number that shows it will keep accepting arguments until it has received this number of arguments or invocings.Syntax:_.curry(fun, [arity=fun.length])Parameters: fun: This is the given function. [ 2 min read Lodash _.curryRight() Method Lodash _.curryRight() method is used to return a curried version of the given function where the given arguments are processed from right to left.Syntax:_.curryRight(fun);Parameters: fun: This is the function that should be used in the current version.Return Value: This method returns the curried fu 2 min read Lodash _.debounce() Method 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 pre 3 min read Lodash _.defer() Method Lodash _.defer() method in lodash is used to defer the calling of the func parameter until the recent call stack is cleared. Moreover, any further arguments are provided to the function parameter of this method when it is called.Syntax:_.defer(func, [args]);Parameters:func: It is the function that i 2 min read Lodash _.delay() Method Lodash _.delay() method is used to call the given function as the parameter after the stated wait time is over, which is in milliseconds. Any further arguments are provided to the function when it is called.Syntax:_.delay(func, wait, args);Parameters: func: It is the function that has to be delayed. 2 min read Like