Apply rate limiting to a function

by krui027

JavaScript

function before(n, func) {
  // 答题
  let count = 0;
  return (...args) => {
    if (count < n) {
      count++;
      console.log(this)
      return func.apply(this, args);
    }
  }
}

const test = before(5, function(a, b) {
  console.log(a, b);
})

for (let i = 0; i < 10; i++) {
  test('我执行了', i);
}