functions-throttle2

by shan10213223

JavaScript

var _ = {};
// _.throttle(function, wait)
// Returns a new, throttled version of the passed function that,
// when invoked repeatedly, will only call the original function
// at most once per every wait milliseconds, and otherwise will
// just return the last computed result. Useful for rate-limiting
// events that occur faster than you can keep up with.
_.throttle = function (func, wait) {
  let now = (new Date()).getTime();
  let newFunc = func;
  
  
  //newFunc.lastResults = func(input);
  newFunc.lastCalled = now;
  return newFunc;
};

// define a function
let myplus = function (n) {
  return n+1;
}

let myplus_throttled = _.throttle(myplus, 5000);
console.log(myplus_throttled(1));
console.log(myplus_throttled.lastCalled);