The lodash _.debounce method for delay of function invoking.

The _.debounce method in lodash is great for delaying the invocation of a method for a certain about of time. In addition it can be canceled, or flushed at once when called.

Basic example of _.debounce

I just need to call it and pass the function that I want debounced, and a time in milliseconds. Once that is done a debounced function will then be returned, once called the function will be invoked once the given about of time passes.

1
2
3
4
5
6
7
var bounced = _.debounce(function(){
console.log('debounced');
}, 30000);
bounced(); // logs 'debounced' after 30 seconds

flushing

A _.debounced method comes with a flush method that can be used to call the method at once.

1
2
3
4
5
6
7
8
9
10
var check = _.debounce(function(){
console.log('checking something...');
check();
},60000);
check();
check.flush(); // check now

conclusion

The _.debounce method in lodash can be useful when making some methods that need to do someting every once in a while, but also need to be check right away. The first and formost thing that comes to mind is some kind of message system.