반응형
250x250
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

짧은코딩

챌린지 10일차 본문

노마드 코더/바닐라 JS로 크롬 앱 만들기

챌린지 10일차

5_hyun 2022. 5. 18. 16:30
const clockTitle = document.querySelector(".js-clock");

function getTime() {
    const xmas = new Date("2022-12-25 00:00:00+0900");
    const today = new Date();
    const dday = xmas - today;
    const day = String(Math.floor(dday / (1000 * 60 * 60 * 24))).padStart(2, "0");
    const hour = String(Math.floor((dday / (1000 * 60 * 60)) % 24)).padStart(2, "0");
    const min = String(Math.floor(((dday / 1000) * 60) % 60)).padStart(2, "0");
    const sec = String(Math.floor((dday / 1000) % 60)).padStart(2, "0");
    clockTitle.innerText = (`${day}d ${hour}h ${min}m ${sec}s`);
}

getTime()
setInterval(getTime, 1000);

setInterval을 이용해서 1초마다 업데이트 되도록 해줬다.  그리고 padStart를 이용하면 한자리 수를 십의 자리까지 나타나게 출력해줄 수 있다.

728x90
반응형

'노마드 코더 > 바닐라 JS로 크롬 앱 만들기' 카테고리의 다른 글

마지막 챌린지(만든지 첫 날)  (0) 2022.05.20
챌린지 11일차  (0) 2022.05.19
챌린지 8일차  (0) 2022.05.16
챌린지 5일차  (0) 2022.05.13
챌린지 4일차  (0) 2022.05.12
Comments