기기의 cpu/gpu등이 지나치게 과열될 때 기기의 손상을 막고자 클럭과 전압을 강제로 낮춤.
프론트에서도 비슷하다.
ex)
예를 들어 사용자가 읽던 포스트의 위치를 기억하고 재방문 시 그 페이지를 보여준다.
js Scroll 이벤트를 이용해서 스크롤단위로 페이지 좌표를 서버에 기록? => 서버부하
function throttle(callback, limit) {
let delay= false;
return function () {
if (!delay) { // limit전까지 if문 실행안함
callback.apply(); // 콜백함수 실행
delay= true;
setTimeout(() => {
delay= false;
}, limit);
}
};
}
window.addEventListener(
"keyup",
throttle(() => {
// 예를들어 서버요청?
}, 500)
);
cf) Leading edge (or “immediate”)를 사용하면 그룹의 첫 이벤트를 발생시킬 수 있다.
function debounce(callback, limit) {
let timeout;
return function () {
clearTimeout(timeout); // 여기서 타임아웃을 삭제하기 때문에 (2)
timeout = setTimeout(() => { // 실행안됨 (3)
callback.apply();
}, limit); // limit이 지나기 전에 반복해서 호출하면 (1)
};
}
inputDebounce.addEventListener(
"keyup",
debounce(function () {
// 처리할 이벤트
}, 500)
);