본문 바로가기
Javascript/Clone Website

[Vanilla-JS] 3. Making a JS Clock part

by 닉우 2020. 8. 2.

시간 정보를 담은 함수를 1초마다 갱신하기 위해 setInterval을 사용할것이다.

 

<코드기록>

const clockContainer = document.querySelector(".js-clock"),
    clockTitle = document.querySelector("h1");

function getTime() {
    const date = new Date();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
    clockTitle.innerText =
        `${hours < 10 ?
            `0${hours}` : hours}:${minutes < 10 ?
                `0${minutes}` : minutes}:${seconds < 10 ?
                    `0${seconds}` : seconds}`;
}


function init() {
    getTime();
    setInterval(getTime, 1000);
}

init();

 

<실행화면>


 

 

 

 

 

 

 

 

 

 

※ 본 포스팅은 개인 공부 기록을 목적으로 남긴 글이며 본 사실과 다른 부분이 있다면 과감하게 지적 부탁드립니다.

댓글