Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

new_bird-hyun

JavaScript - debounce 함수 직접 구현하기 본문

코딩 공부

JavaScript - debounce 함수 직접 구현하기

새혀니 2025. 3. 31. 00:05

function debounce(fn, delay) {
  let timer = null;

  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn(...args);
    }, delay);
  };
}

// 사용 예시
const onSearch = debounce((query) => {
  console.log('Searching for:', query);
}, 300);

onSearch('he');   // 무시
onSearch('hey');  // 무시
onSearch('heyy'); // 300ms 뒤 실행