크롬앱 #5 CLOCK

^^* ,,·2022년 4월 6일
0

5 CLOCK

setInterval

  1. setInterval은 어떤 함수를 일정 간격으로 호출해주는것
  2. setInterval(함수이름, 함수주기);
  3. 함수주기는 1/1000초 단위임으로 1000이 들어가야 1초마다 호출, 3000이들어가면 3초마다 호출

padStart

  • padStart(글자수, 빈자리를 채울 문자)

🔸 html

<h2 id="clock">00:00:00</h2>
🔸 js

const clock = document.querySelector("h2#clock");

function getClock() {
  const data = new Date();
  const hours = String(data.getHours()).padStart(2, "0");
  const Minutes = String(data.getMinutes()).padStart(2, "0");
  const Seconds = String(data.getSeconds()).padStart(2, "0");
  //https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

  clock.innerText = `${hours}:${Minutes}:${Seconds}`;
}

getClock();
setInterval(getClock, 1000);

0개의 댓글