Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash.debounce get previous function's call arguments

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]
like image 734
abel leroyer Avatar asked Oct 28 '25 03:10

abel leroyer


1 Answers

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);
})
like image 137
abel leroyer Avatar answered Oct 29 '25 17:10

abel leroyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!