둘 다 성능 향샹을 위한 것으로 연속되는 이벤트가 발생할 때 필요없이 지나치게 많은 실행이 일어나지 않도록 하는 방식이다.
두 방식이 비슷하지만 다른 부분이 있고 내가 이해한 건 아래와 같다.
대기 상태시에 이벤트가 발생하면 타이머가 작동하고 끝날 때까지 이벤트를 제어함
일반적으로 이벤트 사이 시간이 일정하다는 것이 특징
let timer = 0;
const throttleLeading = (func: () => void, delay: number) : void => {
// 현재 타이머가 있는지 검사, timer는 양수인 정수를 뱉어냄
if(timer !== 0)
return;
func();
timer = setTimeout(()=>{
clearTimeout(timer);
timer = 0;
}, delay);
};
let timer = 0;
let lastFunc : Function;
const throttleTrailing = (func: () => void, delay: number): void => {
lastFunc = func;
console.log(Date.now());
if(timer === 0)
timer = setTimeout(() => {
lastFunc();
clearTimeout(timer);
timer = 0;
}, delay);
};
let timer = 0;
let lastFunc : Function;
const throttleLeadingAndTrailing = (func: () => void, delay: number): void => {
lastFunc = func;
if(timer === 0) {
func();
timer = setTimeout(()=>{
lastFunc();
clearTimeout(timer);
timer = 0;
}, delay);
}
};
일정시간동안 이벤트가 발생하지 않으면 실행
이벤트가 발생할 때마다 타이머가 초기화, 이 타이머가 끝날 때까지 이벤트가 들어오지 않아야 타이머가 정상적으로 종료될 수 있음 → 이벤트가 타이머를 제어
let timer = 0;
const debounceLeading = (func: () => void, delay: number): void => {
if(timer === 0)
func();
clearTimeout(timer);
timer = setTimeout(() => {
clearTimeout(timer);
timer = 0;
}, delay);
};
let timer: number;
const debounceTrailing = (func: () => void, delay: number): void => {
clearTimeout(timer);
timer = setTimeout(() => {
const executionTime = Date.now();
clearTimeout(timer);
func();
}, delay);
};
let timer = 0;
const debounceLeadingAndTrailing = (func: () => void, delay: number): void => {
if(timer === 0)
func();
clearTimeout(timer);
timer = setTimeout(() => {
func();
clearTimeout(timer);
timer = 0
}, delay);
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="add-button">add</button>
<div>값: <b id="value">0</b></div>
<script src="index.js" />
</body>
</html>
위는 테스트 페이지 html
const button = document.getElementById('add-button');
const value = document.getElementById('value');
const plus = () => {
value.innerText = String(Number(value.innerText) + 1);
};
// 실행부분
button.addEventListener('click', () => Debounce.leadingAndTrailing(plus, 2000));
class Debounce {
static timer: number = 0;
static trailing(func: () => void, delay: number) : void {
clearTimeout(this.timer);
this.timer = setTimeout(() => {
const executionTime = Date.now();
clearTimeout(this.timer);
func();
}, delay);
}
static leading(func: () => void, delay: number) : void {
if(this.timer === 0)
func();
clearTimeout(this.timer);
this.timer = setTimeout(() => {
clearTimeout(this.timer);
this.timer = 0;
}, delay);
}
static leadingAndTrailing(func: () => void, delay: number) : void{
if(this.timer === 0)
func();
clearTimeout(this.timer);
this.timer = setTimeout(() => {
func();
clearTimeout(this.timer);
this.timer = 0
}, delay);
}
}
class Throttle {
static timer: number = 0;
static lastFunc: Function;
static trailing (func: () => void, delay: number) : void {
this.lastFunc = func;
if (this.timer === 0)
this.timer = setTimeout(() => {
this.lastFunc();
clearTimeout(this.timer);
this.timer = 0;
}, delay);
}
static leading (func: () => void, delay: number) : void {
if(this.timer !== 0)
return;
func();
this.timer = setTimeout(()=>{
clearTimeout(this.timer);
this.timer = 0;
}, delay);
}
static leadingAndTrailing(func: () => void, delay: number) : void {
this.lastFunc = func;
if(this.timer === 0) {
func();
this.timer = setTimeout(()=>{
this.lastFunc();
clearTimeout(this.timer);
this.timer = 0;
}, delay);
}
}
}