TIL no 7. J.S를 이용해서 시계출력

Daehwi Kim·2020년 4월 30일
0

function getTime()
{
    const date = new Date();
    const minutes = date.getMinutes();
    const hours = date.getHours();
    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();
  • Date 객체를 이용해서 시:분:초 를 만들었다.
  • 시,분,초가 10보다 작을때 두자릿수를 만들기위해 삼항연산자를 이용하였다.
  • 마지막 setInterval함수를 이용해서 홈페이지 새로고침을 누르지않아도 시간이 계속 업데이트 되도록 만들었다.

profile
게으른 개발자

0개의 댓글