Open young-02 opened 8 months ago
빈번하게 발생하는 이벤트를 '특정 시간 이후에 한번만' 실행 시키는 최적화 기법
디바운싱 시간이 300밀리초 설정이 되어있다면 사용자가 클릭을 멈추고 300밀리초가 지나고 나서야 해당 함수가 한번만 실행된다.
사용자가 여러 번 클릭을 하면 마지막 함수만 호출하게 된다.
const clickBtn = doument.querySelector('#clickBtn') const clickCnt = document.querySelector('#clickCnt') const debouncingCnt = document.querySelector('#debouncingCnt') let timerId function debouncing(fnc, timeout = 300){ clearTime(timerId) timerId = setTimeout(func, timeout) } function delayFunction(){ debouncingCnt.innerHTML = parseInt(debouncingCnt.innerHTML) +1 } clickBtn.addEventListener('click', ()=>{ clickCnt.innerHTML = parseInt(clickCnt.innerHTML)+1; debouncing(delayFunction) })
빈번하게 발생하는 이벤트를 '일정한 간격으로 한번만' 실행 시키는 최적화 기법
쓰로틀링은 마지막 함수를 기다리지 않으며, 첫 번째 클릭에 의한 이벤트만을 실행한고 주어진 시간 동안 나머지는 무시한다.
const clickBtn = doument.querySelector('#clickBtn') const clickCnt = document.querySelector('#clickCnt') const debouncingCnt = document.querySelector('#debouncingCnt') let timerId function throttling(func, timout = 300){ if(timerId){ return } timerId = setTimeout(()=>{ func() timerId = undefined },timeout) } function delayFunction(){ throttlingCnt.innerHTML = parseInt(throttlingCnt.innerHTML) +1 } clickBtn.addEventLsitener('click' ,()=>{ clickCnt.innerHTML = paserInt(clickCnt.innerHTML) +1 thottling(delayFunction) })
https://onlydev.tistory.com/151
디바운싱
빈번하게 발생하는 이벤트를 '특정 시간 이후에 한번만' 실행 시키는 최적화 기법
디바운싱 시간이 300밀리초 설정이 되어있다면 사용자가 클릭을 멈추고 300밀리초가 지나고 나서야 해당 함수가 한번만 실행된다.
사용자가 여러 번 클릭을 하면 마지막 함수만 호출하게 된다.
사용
쓰로틀링
빈번하게 발생하는 이벤트를 '일정한 간격으로 한번만' 실행 시키는 최적화 기법
쓰로틀링은 마지막 함수를 기다리지 않으며, 첫 번째 클릭에 의한 이벤트만을 실행한고 주어진 시간 동안 나머지는 무시한다.
사용
차이점
라이브러리