When _.debounce is called multiple times, it only apply with the last call's argument.
var f = _.debounce(function(a) {
    console.log(a);
})
f(12)
f(1223)
f(5)
f(42)
//output -----> 42
Is there a way to get previous function's call arguments as well ?
ex:
 var f = _.customDebounce(function(a) {
    console.log(a);
})
f(12)
f(1223)
f(5)
f(42)
//output -----> [12, 1223, 5, 42]
I finaly made my own implementation of this function, extending lodash:
_.mixin({
  debounceArgs: function(fn, options) {
      var __dbArgs = []
      var __dbFn = _.debounce(function() {
          fn.call(undefined, __dbArgs);
          __dbArgs = []
      }, options);
      return function() {
          __dbArgs.push(_.values(arguments));
          __dbFn();
      }
  },
  throttleArgs: function(fn, options) {
      var _thArgs = []
      var _thFn = _.throttle(function() {
          fn.call(undefined, _thArgs);
          _thArgs = []
      }, options);
      return function() {
          _thArgs.push(_.values(arguments));
          _thFn();
      }
  },
})
Usage:
_.debounceArgs(function(a) {
   console.log(a);
})
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