FORK ME

console 命令行工具 X clear

                    
>
console
const distri = function(fn, space = 300) {
  let last = 0;
  
  const execute = function(...args) {
    const now = Date.now();
    last = last || (now - space);

    const ctx = this;
    const diff = space - now + last;
    if (diff > 0) {
      // console.log('开始延迟');
      setTimeout(() => {
        // console.log('延迟被执行');
        execute.apply(ctx, args);
      }, diff);
    } else {
      fn.apply(ctx, args);
      last = Date.now();
    }
  }
  
  return execute;
}

const say = function(w) {
  console.log('我要说话了', w);
};

const testFun = distri(say, 2000);

let x = 1;

const testtestFun = function() {
  testFun(x);
  x++;
};

document.getElementById('haha')
  .addEventListener('click', testtestFun);
<button id="haha"></button>
#haha {
  width: 100px;
  height: 100px;
}