๋ฐ์ํ
DAY 11
๐ ๋ฏธ์ : ๋๋ฌ์ด ์ฝ๋๋ฅผ ๊ณ ์น์
๐ฉ ๋๋ฌ์ด ์ฝ๋
const merry = document.querySelector(".js-clock");
function getClock() {
const christmas = new Date("2021, 12, 25");
const date = new Date();
const timeGap = christmas - date;
const xDay = Math.floor(timeGap / (1000 * 60 * 60 * 24));
const xHours = Math.floor(
(timeGap - xDay * 1000 * 60 * 60 * 24) / (1000 * 60 * 60)
);
const xMinutes = Math.floor((timeGap % (60 * 60 * 1000)) / (60 * 1000));
const xSeconds = Math.floor((timeGap % (60 * 1000)) / 1000);
const day = String(xDay).padStart(2, "0");
const hours = String(xHours).padStart(2, "0");
const minutes = String(xMinutes).padStart(2, "0");
const seconds = String(xSeconds).padStart(2, "0");
merry.innerText = `${day}d ${hours}h ${minutes}m ${seconds}s`;
}
getClock();
setInterval(getClock, 1000);
๐ค ํด๋ฆฐ ์ฝ๋
setInterval(printXmasTime, 1000);
function printXmasTime() {
const clock = document.querySelector('.js-clock');
const { day, hours, minutes, seconds } = getXmasTime();
clock.innerText = `${day}d ${hours}h ${minutes}m ${seconds}s`;
}
function getXmasTime() {
const xMasDate = new Date('2021, 12, 25');
const date = new Date();
const timeGap = xMasDate - date;
const SET_DAY = 1000 * 60 * 60 * 24;
const SET_HOURS = 1000 * 60 * 60;
const SET_MINUTES = 1000 * 60;
const SET_SECONDS = 1000;
const xMasTime = {
day: setPadStart(Math.floor(timeGap / SET_DAY)),
hours: setPadStart(Math.floor((timeGap - xDay * SET_DAY) / SET_HOURS)),
minutes: setPadStart(Math.floor((timeGap % SET_HOURS) / SET_MINUTES)),
seconds: setPadStart(Math.floor((timeGap % SET_MINUTES) / SET_SECONDS)),
};
return xMasTime;
}
function setPadStart(number) {
return String(number).padStart(2, '0');
}
๐ ๋ฏธ์ ํ๊ธฐ
์ผ๋จ ์ฝ๋์ ๋ก์ง์ ์ต๋ํ ๊ฑด๋ค์ง ์์ผ๋ฉด์ ์ฝ๋๋ฅผ ๊ฐ์ ํ๋ ค๊ณ ํ๋ค.
์๋ํ๋ฉด ๋ชจ๋ ๊ณ ์ณ๋ฒ๋ฆฌ๋ฉด ๋ฆฌํฉํ ๋ง์ด ์๋๋ผ ๊ทธ๋ฅ ๋ด๊ฐ ์๋ก ์ง๋ ๋๋์ด๋ผ..?
'ํด๋ฆฐ ์ฝ๋'์ฑ ์์ ๋ฐฐ์ด ๋ด์ฉ๊ณผ ๊ฐ์ด ๋ด๊ฐ ์ ์ฉํ ๋ด์ฉ์
- ํจ์๋ ์ต๋ํ ํ๋์ ๊ธฐ๋ฅ๋ง ํ๋๋ก ๋ถ๋ฆฌํจ
- ์ค๋ณต๋๋ ๋ด์ฉ์ ํ๋์ ํจ์๋ก ์์ฑํจ
- ์ ๋ฌธ์ ์ฝ๋ฏ์ด ์์์ ์๋๋ก ์์ฐ์ค๋ฝ๊ฒ ์ฝ์ ์ ์๋๋ก ํจ์์ ์์๋ฅผ ๋ณ๊ฒฝํจ
- ๋ณ์, ํจ์ ์ด๋ฆ ๋ด์ฉ ์ข ๋ ๋ช ํํ๊ฒ ๋ณ๊ฒฝํจ
- ์ฌ์ฉํ ๋ณ์ ์์น์ ํด๋น ๋ณ์์ ์์น๋ฅผ ๊ฐ๊น๊ฒ ์์น์ํด
๋ฐ์ํ