手写代码之节流

function throttle(func, ms = 1000) {
  let canRun = true;
  return function (...args) {
    if (!canRun) return;
    canRun = false;
    setTimeout(() => {
      func.apply(this, args);
      canRun = true;
    }, ms);
  };
}

 

posted @ 2024-01-30 17:31  行走的蒲公英  阅读(4)  评论(0编辑  收藏  举报