Aldarund
Aldarund

Reputation: 17621

_.debounce doesnt work as expected

I want to use lodash debounce() function. Here is the simplest example.

var update_from = function (name) {
            console.log(name);
 };
$( document ).ready(function() {
  _.debounce(update_from, 1500)("first");
  _.debounce(update_from, 1500)('second');
  _.debounce(update_from, 1500)("third");
});

I expect only "third" to be printed into console. But all three are printed.

What am I doing wrong or misunderstanding? According this article, this should work like I expect, but it doesn't.

Here is plunkr with that example: http://plnkr.co/edit/OCDAChkMes97XcMLJSag?p=preview

Upvotes: 1

Views: 762

Answers (1)

barry-johnson
barry-johnson

Reputation: 3224

You're creating and executing three separate debounced functions, not one debounced function three times.

var myFunc = _.debounce(update_from, 1500);
myFunc('first');
myFunc('second');
myFunc('third');

Upvotes: 3

Related Questions