Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debounce function with args underscore

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); } 
like image 891
Gandalf StormCrow Avatar asked May 25 '14 17:05

Gandalf StormCrow


People also ask

What is _ Debounce?

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.

What is the difference between Debounce and throttle?

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.

What is debounce and how could you implement debounce?

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.


1 Answers

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

like image 58
Jaime Agudo Avatar answered Oct 11 '22 09:10

Jaime Agudo