๐Ÿ’– 41.1 ํ˜ธ์ถœ ์Šค์ผ€์ค„๋ง

ํ•จ์ˆ˜๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ์ฆ‰์‹œ ์‹คํ–‰๋œ๋‹ค. ๋งŒ์•ฝ ํ•จ์ˆ˜๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ์ผ์ • ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผ๋œ ์ดํ›„์— ํ˜ธ์ถœ๋˜๋„๋ก ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์˜ˆ์•ฝํ•˜๋ ค๋ฉด ํƒ€์ด๋จธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋ฅผ ํ˜ธ์ถœ ์Šค์ผ€์ค„๋ง์ด๋ผ ํ•œ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํƒ€์ด๋จธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ด๋จธ ํ•จ์ˆ˜ setTimeout๊ณผ setInterval, ํƒ€์ด๋จธ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ด๋จธ ํ•จ์ˆ˜ clearTimeout๊ณผ clearInterval์„ ์ œ๊ณตํ•œ๋‹ค.

ํƒ€์ด๋จธ ํ•จ์ˆ˜๋Š” ํ˜ธ์ŠคํŠธ ๊ฐ์ฒด์ด๋‹ค. ํƒ€์ด๋จธ ํ•จ์ˆ˜ setTimeout๊ณผ setInterval์ด ์ƒ์„ฑํ•œ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ ๋‹จ ํ•˜๋‚˜์˜ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ ์Šคํƒ์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ํƒœ์Šคํฌ๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•  ์ˆ˜ ์—†๋‹ค. ์ฆ‰, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ๋กœ ๋™์ž‘ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ setTimeout๊ณผ setInterval ํƒ€์ด๋จธ ํ•จ์ˆ˜ set์€ ๋น„๋™๊ธฐ(asynvhronous) ์ฒ˜๋ฆฌ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

๐Ÿ’– 41.2 ํƒ€์ด๋จธ ํ•จ์ˆ˜

โœ” 41.2.1 **setTimeout** / **clearTimeout**

setTimeout ํ•จ์ˆ˜๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์‹œ๊ฐ„์œผ๋กœ ๋‹จ ํ•œ ๋ฒˆ ๋™์ž‘ํ•˜๋Š” ํƒ€์ด๋จธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ดํ›„ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋˜๋ฉด ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ์ฆ‰, setTimeout ํ•จ์ˆ˜์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์‹œ๊ฐ„ ์ดํ›„๋กœ ๋‹จ ํ•œ ๋ฒˆ ์‹คํ–‰๋˜๋„๋ก ํ˜ธ์ถœ ์Šค์ผ€์ผ๋ง ํ•œ๋‹ค.

// 1์ดˆ(1000ms) ํ›„ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
setTimeout(() => console.log('Hi!'), 1000);

// 1์ดˆ(1000ms) ํ›„ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
// ์ด๋•Œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— 'Lee'๊ฐ€ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ๋‹ค.
setTimeout(name => console.log(`Hi! ${name}.`), 1000, 'Lee');

// ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜(delay)๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๊ธฐ๋ณธ๊ฐ’ 0์ด ์ง€์ •๋œ๋‹ค.
setTimeout(() => console.log('Hello!'));
// 1์ดˆ(1000ms) ํ›„ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
// setTimeout ํ•จ์ˆ˜๋Š” ์ƒ์„ฑ๋œ ํƒ€์ด๋จธ๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์œ ํ•œ ํƒ€์ด๋จธ id๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const timerId = setTimeout(() => console.log('Hi!'), 1000);

// setTimeout ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ํƒ€์ด๋จธ id๋ฅผ clearTimeout ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜์—ฌ ํƒ€์ด๋จธ๋ฅผ
// ์ทจ์†Œํ•œ๋‹ค. ํƒ€์ด๋จธ๊ฐ€ ์ทจ์†Œ๋˜๋ฉด setTimeout ํ•จ์ˆ˜์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
clearTimeout(timerId);

โœ” 41.2.2 **setInterval** / **clearInterval**

setInterval ํ•จ์ˆ˜๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์‹œ๊ฐ„์„ ๋ฐ˜๋ณต ๋™์ž‘ํ•˜๋Š” ํƒ€์ด๋จธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ดํ›„ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋ ๋•Œ๋งˆ๋‹ค ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜๋ณต ํ˜ธ์ถœ๋œ๋‹ค. ์ด๋Š” ํƒ€์ด๋จธ๊ฐ€ ์ทจ์†Œ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค.

let count = 1;

// 1์ดˆ(1000ms) ํ›„ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋  ๋•Œ๋งˆ๋‹ค ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
// setInterval ํ•จ์ˆ˜๋Š” ์ƒ์„ฑ๋œ ํƒ€์ด๋จธ๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์œ ํ•œ ํƒ€์ด๋จธ id๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const timeoutId = setInterval(() => {
  console.log(count); // 1 2 3 4 5
  // count๊ฐ€ 5์ด๋ฉด setInterval ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ํƒ€์ด๋จธ id๋ฅผ clearInterval ํ•จ์ˆ˜์˜
  // ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜์—ฌ ํƒ€์ด๋จธ๋ฅผ ์ทจ์†Œํ•œ๋‹ค. ํƒ€์ด๋จธ๊ฐ€ ์ทจ์†Œ๋˜๋ฉด setInterval ํ•จ์ˆ˜์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€
  // ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
  if (count++ === 5) clearInterval(timeoutId);
}, 1000);

๐Ÿ’– 41.2.3 ๋””๋ฐ”์šด์Šค์™€ ์Šค๋กœํ‹€

scroll, resize, input, mousemove ๊ฐ™์€ ์ด๋ฒคํŠธ๋“ค์€ ์งง์€ ์‹œ๊ฐ„๋™์•ˆ ์—ฐ์†์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”์ธ๋”ฉ๋œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋Š” ๊ณผ๋„ํ•˜๊ฒŒ ํ˜ธ์ถœ๋˜์–ด ์„ฑ๋Šฅ์ด ์•…ํ™”๋  ์ˆ˜ ์žˆ๋‹ค. ๋””๋ฐ”์šด์Šค์™€ ์Šค๋กœํ‹€์€ ์งง์€ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์—ฐ์†ํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•ด์„œ ๊ณผ๋„ํ•œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์˜ ํ˜ธ์ถœ์„ ๋ฐฉ์ง€ํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์ด๋‹ค.

<!DOCTYPE html>
<html>
<body>
  <button>click me</button>
  <pre>์ผ๋ฐ˜ ํด๋ฆญ ์ด๋ฒคํŠธ ์นด์šดํ„ฐ    <span class="normal-msg">0</span></pre>
  <pre>๋””๋ฐ”์šด์Šค ํด๋ฆญ ์ด๋ฒคํŠธ ์นด์šดํ„ฐ <span class="debounce-msg">0</span></pre>
  <pre>์Šค๋กœํ‹€ ํด๋ฆญ ์ด๋ฒคํŠธ ์นด์šดํ„ฐ   <span class="throttle-msg">0</span></pre>
  <script>
    const $button = document.querySelector('button');
    const $normalMsg = document.querySelector('.normal-msg');
    const $debounceMsg = document.querySelector('.debounce-msg');
    const $throttleMsg = document.querySelector('.throttle-msg');

    const debounce = (callback, delay) => {
      let timerId;
      return event => {
        if (timerId) clearTimeout(timerId);
        timerId = setTimeout(callback, delay, event);
      };
    };

    const throttle = (callback, delay) => {
      let timerId;
      return event => {
        if (timerId) return;
        timerId = setTimeout(() => {
          callback(event);
          timerId = null;
        }, delay, event);
      };
    };

    $button.addEventListener('click', () => {
      $normalMsg.textContent = +$normalMsg.textContent + 1;
    });

    $button.addEventListener('click', debounce(() => {
      $debounceMsg.textContent = +$debounceMsg.textContent + 1;
    }, 500));

    $button.addEventListener('click', throttle(() => {
      $throttleMsg.textContent = +$throttleMsg.textContent + 1;
    }, 500));
  </script>
</body>
</html>

โœ”41.3.1 ๋””๋ฐ”์šด์Šค

๋””๋ฐ”์šด์Šค๋Š” ์งง์€ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์—ฐ์†ํ•ด์„œ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋‹ค๊ฐ€ ์ผ์ • ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผํ•œ ์ดํ›„์— ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋˜๋„๋ก ํ•œ๋‹ค. ์ฆ‰, ๋””๋ฐ”์šด์Šค๋Š” ์งง์€ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•ด์„œ ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ๋งŒ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ํ˜ธ์ถœ๋˜๋„๋ก ํ•œ๋‹ค.

<!DOCTYPE html>
<html>
<body>
  <input type="text">
  <div class="msg"></div>
  <script>
    const $input = document.querySelector('input');
    const $msg = document.querySelector('.msg');

    const debounce = (callback, delay) => {
      let timerId;
      // debounce ํ•จ์ˆ˜๋Š” timerId๋ฅผ ๊ธฐ์–ตํ•˜๋Š” ํด๋กœ์ €๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      return event => {
        // delay๊ฐ€ ๊ฒฝ๊ณผํ•˜๊ธฐ ์ด์ „์— ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด์ „ ํƒ€์ด๋จธ๋ฅผ ์ทจ์†Œํ•˜๊ณ 
        // ์ƒˆ๋กœ์šด ํƒ€์ด๋จธ๋ฅผ ์žฌ์„ค์ •ํ•œ๋‹ค.
        // ๋”ฐ๋ผ์„œ delay๋ณด๋‹ค ์งง์€ ๊ฐ„๊ฒฉ์œผ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด callback์€ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.
        if (timerId) clearTimeout(timerId);
        timerId = setTimeout(callback, delay, event);
      };
    };

    // debounce ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํด๋กœ์ €๊ฐ€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋กœ ๋“ฑ๋ก๋œ๋‹ค.
    // 300ms๋ณด๋‹ค ์งง์€ ๊ฐ„๊ฒฉ์œผ๋กœ input ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด debounce ํ•จ์ˆ˜์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š”
    // ํ˜ธ์ถœ๋˜์ง€ ์•Š๋‹ค๊ฐ€ 300ms ๋™์•ˆ input ์ด๋ฒคํŠธ๊ฐ€ ๋” ์ด์ƒ ๋ฐœ์ƒํ•˜๋ฉด ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋œ๋‹ค.
    $input.oninput = debounce(e => {
      $msg.textContent = e.target.value;
    }, 300);
  </script>
</body>
</html>

์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ์„ ์™„๋ฃŒํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋Š” ์ •ํ™•ํžˆ ์•Œ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ผ์ • ์‹œ๊ฐ„ ๋™์•ˆ ํ…์ŠคํŠธ ์ž…๋ ฅ ํ•„๋“œ์— ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด ์ž…๋ ฅ์ด ์™„๋ฃŒ๋œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด debounce ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ํ•จ์ˆ˜๋Š” debounce ํ•จ์ˆ˜์— ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ์‹œ๊ฐ„(delay)๋ณด๋‹ค ์งง์€ ๊ฐ„๊ฒฉ์œผ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด์ „ ํƒ€์ด๋จธ๋ฅผ ์ทจ์†Œํ•˜๊ณ  ์ƒˆ๋กœ์šด ํƒ€์ด๋จธ๋ฅผ ์žฌ์„ค์ •ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ delay ๋ณด๋‹ค ์งง์€ ๊ฐ„๊ฒฉ์œผ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์—ฐ์†ํ•ด์„œ ๋ฐœ์ƒํ•˜๋ฉด debounce ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๋‹ค๊ฐ€ delay ๋™์•ˆ input ์ด๋ฒคํŠธ๊ฐ€ ๋” ์ด์ƒ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋œ๋‹ค.

โœ”41.3.2 ์Šค๋กœํ‹€

์Šค๋กœํ‹€์€ ์งง์€ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์—ฐ์†ํ•ด์„œ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ์ผ์ • ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์ตœ๋Œ€ ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋˜๋„๋ก ํ•œ๋‹ค. ์ฆ‰, ์Šค๋กœํ‹€์€ ์งง์€ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์—ฐ์†ํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•ด์„œ ์ผ์ • ์‹œ๊ฐ„ ๋‹จ์œ„๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ํ˜ธ์ถœ๋˜๋„๋ก ํ˜ธ์ถœ ์ฃผ๊ธฐ๋ฅผ ๋งŒ๋“ ๋‹ค.

<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      width: 300px;
      height: 300px;
      background-color: rebeccapurple;
      overflow: scroll;
    }

    .content {
      width: 300px;
      height: 1000vh;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="content"></div>
  </div>
  <div>
    ์ผ๋ฐ˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ scroll ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•œ ํšŸ์ˆ˜:
    <span class="normal-count">0</span>
  </div>
  <div>
    ์Šค๋กœํ‹€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ scroll ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•œ ํšŸ์ˆ˜:
    <span class="throttle-count">0</span>
  </div>

  <script>
    const $container = document.querySelector('.container');
    const $normalCount = document.querySelector('.normal-count');
    const $throttleCount = document.querySelector('.throttle-count');

    const throttle = (callback, delay) => {
      let timerId;
      // throttle ํ•จ์ˆ˜๋Š” timerId๋ฅผ ๊ธฐ์–ตํ•˜๋Š” ํด๋กœ์ €๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      return event => {
        // delay๊ฐ€ ๊ฒฝ๊ณผํ•˜๊ธฐ ์ด์ „์— ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋‹ค๊ฐ€
        // delay๊ฐ€ ๊ฒฝ๊ณผํ–ˆ์„ ๋•Œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ƒˆ๋กœ์šด ํƒ€์ด๋จธ๋ฅผ ์žฌ์„ค์ •ํ•œ๋‹ค.
        // ๋”ฐ๋ผ์„œ delay ๊ฐ„๊ฒฉ์œผ๋กœ callback์ด ํ˜ธ์ถœ๋œ๋‹ค.
        if (timerId) return;
        timerId = setTimeout(() => {
          callback(event);
          timerId = null;
        }, delay, event);
      };
    };

    let normalCount = 0;
    $container.addEventListener('scroll', () => {
      $normalCount.textContent = ++normalCount;
    });

    let throttleCount = 0;
    // throttle ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํด๋กœ์ €๊ฐ€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋กœ ๋“ฑ๋ก๋œ๋‹ค.
    $container.addEventListener('scroll', throttle(() => {
      $throttleCount.textContent = ++throttleCount;
    }, 100));
  </script>
</body>
</html>
profile
์ด์‚ฌ์ค‘์ž…๋‹ˆ๋‹ค!๐ŸŒŸhttps://velog.io/@devkyoung2

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด