I've got a function which takes in some arguments. But usage of underscore debounce is :
var lazyLayout = _.debounce(calculateLayout, 300);
But in my case calculateLayout
needs some arguments to run. How can I pass them in this case?
Update :
Sample calculateLayout
function :
var calculateLayout = function(a,b) { console.log('a is ' + a + ' and b is ' + b); }
The _. debounce() method of Function in lodash is used to create a debounced function which delays the given func until after the stated wait time in milliseconds have passed since the last time this debounced function was called.
The major difference between debouncing and throttling is that debounce calls a function when a user hasn't carried out an event in a specific amount of time, while throttle calls a function at intervals of a specified amount of time while the user is carrying out an event.
The debounce() function forces a function to wait a certain amount of time before running again. The function is built to limit the number of times a function is called. The Send Request() function is debounced. Requests are sent only after fixed time intervals regardless of how many times the user presses the button.
You don't need an anonymous function in the middle, arguments will automatically be passed to the original function when you run the debounced version.
var debounceCalculate = _.debounce(calculateLayout, 300); debounceCalculate(a,b);
As an advantage you don't have to hardcode-bind the arguments in advance
You can try it and if curious just check the source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With