leecom116

제이의 기술 블로그
  • Print (216)
    • Frontend (29)
      • React (3)
      • HTML, CSS (8)
      • JavaScript (11)
      • JQuery (4)
      • JSP (2)
    • Backend (59)
      • Java (31)
      • Spring (11)
      • Spring Boot (0)
      • Python (1)
      • Node.js (1)
      • C, C++ (12)
      • Linux (2)
    • Database (3)
      • MariaDB (0)
      • Oracle (0)
      • MySQL (0)
    • Project (1)
    • Algorithm (67)
      • 백준 (43)
      • 프로그래머스 (0)
      • 이코테 (6)
      • 코드업 (17)
    • Tool (2)
      • Git (1)
      • Log (1)
    • CS (4)
    • Tech Interview (18)
      • Java (9)
      • Web (9)
    • Study (31)
      • 인프런 (3)
      • 정보처리기사 (8)
      • util (4)
      • 쌍용 (14)

깃허브

    https://github.com/leecom116

«   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

최근 글

인기 글

블로그 메뉴

  • 홈
  • 태그
  • 방명록

태그

  • Associate
  • HTML
  • man month
  • 산술 변환
  • 웹 개발
  • 의존성 주입
  • Web Developer
  • char
  • 코드업 2차원 배열
  • C언어 프로젝트
  • 코드업 기초100제
  • 코드업
  • JSP
  • CSS
  • 자바의 정석
  • 델파이 기법
  • 웹 개발 부트캠프
  • 메타포어
  • 웹개발
  • 자바 인터페이스
  • 자바 예외 처리
  • 나도코딩
  • 코드업 기초
  • 객체 배열
  • 논리 연산자
  • static메서드
  • 참조형 변수
  • c언어
  • 나선형 모델
  • 정보공학 방법론

최근 댓글

hELLO · Designed By 정상우.
leecom116
Frontend/JavaScript

[JavaScript] 디바운싱과 쓰로틀링

2024. 1. 31. 23:59

디바운싱

- 연속적으로 호출되는 함수들 중에서 처음 or 마지막에 호출되는 함수만 실행하는 방식(함수는 한 번만 실행)

- 주로 이벤트 핸들러에 적용

 

// #1 타이머 예제
let timer;
document.querySelector('#input').addEventListener('input', function(e) {
    if(timer) {
        clearTimeout(timer);
    }
    timer = setTimeout(function() {
        // 실행할 코드 내용
    }, 200);
});


// #2 텍스트 입력 예제
function debounce(func, delay) {
    let timeoutId;
    return function() {
        const context = this;
        const args = arguments;
        clearTimeout(timeoutId);
        timeoutId = setTimeout(() => {
            func.apply(context, args);
        }, delay);
    };
}

const inputField = document.getElementById('inputField');
inputField.addEventListener('input', debounce(function() {
    // 실행할 코드 내용
}, 300));

 

 

 

쓰로틀링

- 마지막 함수가 호출된 후 일정 시간이 지나기 전에 다시 호출되지 않도록 하는 방식

- 일정 시간 간격마다 함수를 실행하도록 제어

 

// #1 타이머 예제
let timer;
document.querySelector('.body').addEventListener('scroll', function (e) {
  if (! timer) {
    timer = setTimeout(function() {
      timer = null;
      // 실행할 코드 내용
    }, 200);
  }
});


// #2 마우스 이동
function throttle(func, delay) {
    let throttleTimeout;
    return function() {
        const context = this;
        const args = arguments;
        if (!throttleTimeout) {
            throttleTimeout = setTimeout(() => {
                func.apply(context, args);
                throttleTimeout = null;
            }, delay);
        }
    };
}

window.addEventListener('mousemove', throttle(function(event) {
    // 실행할 코드 내용
}, 300));
저작자표시

'Frontend > JavaScript' 카테고리의 다른 글

$(document).ready() 와 window.onload 의 차이점  (0) 2024.07.01
자바스크립트의 동작 원리  (0) 2024.02.03
window 객체의 opener 사용하기  (0) 2023.10.15
자바스크립트 null, undefined, NaN의 차이  (0) 2023.10.08
크롬 브라우저에서 자바스크립트 디버깅 하는 법  (0) 2023.08.27
    'Frontend/JavaScript' 카테고리의 다른 글
    • $(document).ready() 와 window.onload 의 차이점
    • 자바스크립트의 동작 원리
    • window 객체의 opener 사용하기
    • 자바스크립트 null, undefined, NaN의 차이
    leecom116
    leecom116

    티스토리툴바