ํฐ์คํ ๋ฆฌ ๋ทฐ
Infinite Scroll
Question 1. ์ต์ด์๋ 20๊ฐ์ ๋ชฉ๋ก์ ๋ถ๋ฌ์จ๋ค. 2. ์คํฌ๋กค์ ์ตํ๋จ์ผ๋ก ์ด๋์ 'loading' ์ํ ํ์๊ฐ ๋ํ๋๋ฉฐ, ์ดํ์ 20๊ฐ ๋ชฉ๋ก์ ๋ ๊ฐ์ ธ์จ๋ค. 3. ๋ก๋ฉ ์๋ฃ์ 'loading'ํ์๊ฐ ์ฌ๋ผ์ง๋ฉฐ, ๊ฐ์ ธ์จ ๋ชฉ๋ก
choi95.tistory.com
์ด์ ํฌ์คํ ์์ onScroll ์ด๋ฒคํธ์ scrollingElement ํ๋กํผํฐ๋ฅผ ํตํด ๋ฌดํ ์คํฌ๋กค ๋ทฐ๋ฅผ ๊ตฌํํ์๋ค.
ํ์ง๋ง ์ด์ ๊ฐ์ด onScroll ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ก ์ด๋ฒคํธ๋ฅผ ์ง์์ ์ผ๋ก ๊ฐ์งํ ๊ฒฝ์ฐ ์ํํ๊ณ ์ ํ๋ ์ฐ์ฐ ์ฒ๋ฆฌ๋ค์ด ์คํฌ๋กค ์ด๋ฒคํธ๊ฐ
๋ฐ์ํ ๋๋ง๋ค ์ํ๋๋ค๋ ๋ฌธ์ ๊ฐ ์๋ค.
Throttle๊ณผ Debounce
์ด๋ฌํ ๋ฐ๋ณต์ ์ธ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
- Throttle: ์ผ์ ์๊ฐ๊ฐ๊ฒฉ์ผ๋ก ํ๋ฒ์ฉ๋ง ์คํ
- Debounce: ๋ง์ง๋ง์ผ๋ก ํ ๋ฒ๋ง ์ํ
setTimeout()๊ณผ clearTimeout() ํจ์๋ฅผ ์ด์ฉํ Debounce() ํจ์๋ฅผ ๊ตฌํํ์ฌ ์ฃผ์ด์ง ์ผ์ ์๊ฐ ๋์ ๋ง์ง๋ง์ผ๋ก ์์ฒญํ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ง์ ์ํํ๋๋ก ํ๋ค.
(clearTimeout()์ ํตํด ๋ฐ๋ณต ์ํ์ ๋ง๊ธฐ ์ํด ์ฌ์ ์ ์๋ณ ๋ณ์๋ฅผ null๋ก ์ ์ธํ์ฌ ์ฃผ๊ณ ํด๋ก์ ๋ฅผ ํตํด ์ด๋ฅผ ํ์ฉํ๋ค)
Code
export const debounce = (func, delay) => {
let timeoutId = null;
return function (...args) {
const context = this;
if(timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => func.apply(context, args), delay);
}
};
'JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Dark-mode (0) | 2021.07.02 |
---|---|
Intersection Observer API๋ฅผ ํตํ ์ด๋ฒคํธ ์ต์ ํ (0) | 2021.07.01 |
Infinite Scroll (0) | 2021.07.01 |
ScrollSpy_offsetTop, scrollTop, clientHeight (0) | 2021.06.03 |
Context Menu (0) | 2021.06.01 |