코딩 공부

JavaScript - 함수 실행 제한 (throttle 구현)

새혀니 2025. 4. 15. 12:28

function throttle(fn, delay) {
  let lastCall = 0;
  return (...args) => {
    const now = new Date().getTime();
    if (now - lastCall < delay) return;
    lastCall = now;
    fn(...args);
  };
}

// 사용 예
const onScroll = throttle(() => console.log('스크롤 중!'), 500);