ํฐ์คํ ๋ฆฌ ๋ทฐ
๊ธฐ์กด ์ฝ๋
stopwatch
Question ์์ ๊ฐ์ด ์๋ํ๋ ์คํฑ์์น๋ฅผ ๊ตฌํํ์์ค ์ฃผ์ Solution_stopwatch ์๊ฐ ๊ตฌํ ๋ฏธ๋ฆฌ์ด_ms๋ง๋ค(0~99๋ฏธ๋ฆฌ์ด๋ 100๋ถ์ 1์ด => 100) setInterval() ํจ์ ๋ด์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ๋ค. ํด๋น ์ฝ๋ฐฑ ํจ์๋ฅผ..
choi95.tistory.com
์ด์ stopwatch๋ฅผ ๊ตฌํํ ๋ ๊ธฐ๋กํ LapTime์ ํ๋ฉด ์์ ๋๋๋งํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ก ๊ตฌํํ์๋ค.
$laps.style.display = 'grid';
let newLap = document.createElement("div");
newLap.setAttribute("class", "laps");
newLap.style.display = 'grid';
let newLapNum = document.createElement("div");
newLapNum.setAttribute("class", "lap-title");
newLapNum.innerHTML = `${laps.length}`
let time = document.createElement("div");
time.setAttribute("class", "lap-title");
time.innerHTML = `${laps[indexLaps]}`;
newLap.appendChild(newLapNum);
newLap.appendChild(time);
document.querySelector('.stopwatch').appendChild(newLap);
createElement()์ setAttribute()๋ฅผ ํตํด JS ๋ด์์ ๋์ ์ผ๋ก laps ํ๊ทธ๋ฅผ ์์ฑํ์๋ค.
์์ ๊ฐ์ด laps ํ๊ทธ๋ฅผ ๋ง๋ค๊ฒ ๋๋ฉด ์คํฑ์์น์์ Reset ๋ฒํผ์ ํด๋ฆญํ์์ ๋ ํด๋น laps ํด๋์ค๋ช ์ผ๋ก ๋ ํ๊ทธ๋ค์ ๋ชจ๋ ์ญ์ ํด์ค์ผ ํ๋ค.
// Reset Event
let lapsAll = document.querySelectorAll('.stopwatch > .laps');
lapsAll.forEach((lap, i) => {
if(i === 0) {
lap.style.display = 'none';
}
else {
lap.remove();
}
});
ํ์ง๋ง ์ด๋ ๊ฒ ๋๋ฉด Reset Event ๋ด์์ ๋ชจ๋ laps๋ก ๋ ํด๋์ค ํ๊ทธ๋ค์ ๋ฐฐ์ด ์์์ ์ํ ๋ฐ ์ฐธ์กฐํด์ผ ๋๋ ์ข ์์ฑ์ด ๋ฐ์ํ๋ค.
(๋ณธ๋๋ผ๋ฉด lapTime๊ณผ ๊ด๋ จ ๋ ํฌํผ ํจ์ ๋ด์์ ๊ด๋ จ ์ฐ์ฐ์ ์ํํด์ค์ผ ํ๋๊ฒ ํจ์จ์ ์ธ ์ปดํฌ๋ํธ ์ธก๋ฉด์์ ์ข์ ๊ฒ์ด๋ค)
<์ด๋ ์ฒ์์ ๋์ค๋ ์นดํ ๊ณ ๋ฆฌ ์ญํ ์ lap-title ํ๊ทธ_Reset์ด ๋์ด๋ DOM ์์์ ์ฌ๋ผ์ง๋ฉด ์๋๊ณ display ์์ฑ ๊ฐ๋ง none์ผ๋ก ๋ฐ๊พธ์ด ํ๋ฉด ์์ ๋ณด์ด์ง ์๊ฒ ๋๋๋งํด์ผ ํจ ์ ์ดํ์ ์ฌ์ฉ์๊ฐ lap๋ฒํผ์ ํด๋ฆญํ์์ ๋ ๋์ค๋ lapTime ๊ธฐ๋ก ๊ฐ์ ๋ณ๋์ ์ฒ๋ฆฌ๊ฐ ํ์>
createDocumentFragment()๋ฅผ ํตํ Refactoring
ํด๋น ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด createDocumentFragment()๋ฅผ ์ฌ์ฉํ๋ค.
DocumentFragments ๋ DOM ๋ ธ๋๋ค ์ ๋๋ค. ์ด๊ฒ๋ค์ ๋ฉ์ธ DOM ํธ๋ฆฌ์ ์ผ๋ถ๊ฐ ๋์ง ์์ต๋๋ค. ์ผ๋ฐ์ ์ธ ์ ์ฆ ์ผ์ด์ค๋ ๋คํ๋จผํธ ์กฐ๊ฐ์ ์์ฑํ๊ณ , ์๋ฆฌ๋จผํธ๋ค์ ๋คํ๋จผํธ ์กฐ๊ฐ์ ์ถ๊ฐํ๊ณ ๊ทธ ๋คํ๋จผํธ ์กฐ๊ฐ์ DOM ํธ๋ฆฌ์ ์ถ๊ฐํ๋ ๊ฒ์ ๋๋ค. DOM ํธ๋ฆฌ ๋ด์์ ๋คํ๋จผํธ ์กฐ๊ฐ์ ๊ทธ๊ฒ์ ๋ชจ๋ ์์๋ค๋ก ๋์ฒด๋ฉ๋๋ค.
const createLapElement = (newLap, index) => {
const $fragment = document.createDocumentFragment();
const $index = document.createElement('div');
$index.textContent = index;
$fragment.appendChild($index);
const $newLab = document.createElement('div');
$newLab.textContent = formatElapsedTime(newLap);
$fragment.appendChild($newLab);
$laps.appendChild($fragment);
$laps.style.display = 'grid';
};
DocumentFragment()๋ฅผ ํตํด lapTime์ ๊ธฐ๋กํ๋ ๋ div ํ๊ทธ๋ฅผ Wrapper๋ก์ ๊ฐ์ธ์ฃผ๊ณ ํด๋น Wrapper๋ฅผ laps ํ๊ทธ์ appendChild๋ฅผ ์ด์ฉํ์ฌ DOM ์์ ์์๋ก ์ถ๊ฐํด ์ฃผ์๋ค.
๊ทธ๋ฆฌ๊ณ ๋ค์๊ณผ ๊ฐ์ด laps ํด๋์ค๋ก ๋ ํด๋์ค๋ฅผ ์ง์ ์ญ์ ํ๋ ๊ฒ์ด ์๋๋ผ ํด๋น laps ํ๊ทธ์ ์์ ์์๋ค ์ค CSS ๊ฐ์ ์ ํ์ not์
์ด์ฉํ์ฌ .lap-title ํด๋์ค๋ฅผ ์ ์ธ ํ ๋ชจ๋ ํ๊ทธ๋ค(๋คํ๋จผํธ ์กฐ๊ฐ_๋ ๊ฐ์ div ํ๊ทธ๋ก ๊ตฌ์ฑ)์ ์ญ์ ํ๊ฒ ํ์๋ค.
/ ๋ฉ ํ์์ ์ด๊ธฐํ(DOM์์ ๋ชจ๋ ์ ๊ฑฐ)ํ๋ค.
const removeAllLapElement = () => {
document.querySelectorAll('.laps > div:not(.lap-title)').forEach($lap => $lap.remove());
$laps.style.display = 'none';
};
'JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Image ์ฌ์ด์ฆ ๊ฐ์ ธ์ค๊ธฐ_JS ํ์ฑ๊ณผ ์คํ ๋์ ์์ (0) | 2021.07.10 |
---|---|
JS ์์์ HTML ๋์ ์์ฑ (0) | 2021.07.07 |
stopwatch (0) | 2021.07.05 |
window.matchMedia ๋ฉ์๋ (0) | 2021.07.02 |
Dark-mode (0) | 2021.07.02 |