Johar Zaman
Johar Zaman

Reputation: 2103

How to debounce a function inside modelChange function using lodash debounce?

I have a function which invokes whenever the model changed. I want to use debounced function inside the modelChange function.

I am using lodash debounce but it is not invoking the function what am i doing wrong?

modelChange Function:

 onModelChange(model) {    
    _.debounce(function() {
      alert('debouned');
    }, 2000)
  }

Here is link to Stackbiltz

Upvotes: 1

Views: 512

Answers (1)

Vadim
Vadim

Reputation: 3439

Creating method for debouncing and calling it inside of onModelChange should do the trick:

debouncedOnChange = _.debounce(function() {
  alert('debounced');
}, 2000);

onModelChange(model) {
  // alert('model has been changed');
  this.debouncedOnChange();
}

stackblitz

Upvotes: 1

Related Questions