- Intervals
const clock = document.querySelector("h2#clock");
function sayHello(){
console.log("hello");
}
setInterval(sayHello, 5000);
- clock 구현
const clock = document.querySelector("h2#clock");
function getClock(){
const date = new Date();
clock.innerText=`${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
}
getClock();
setInterval(getClock,1000);
- padStart 사용해서 원하는 clock 형태로 출력
const clock = document.querySelector("h2#clock");
function getClock(){
const date = new Date();
const hours = String(date.getHours()).padStart(2,"0");
const minutes = String(date.getMinutes()).padStart(2,"0");
const seconds = String(date.getSeconds()).padStart(2,"0");
clock.innerText= `${hours}:${minutes}:${seconds}`;
}
getClock();
setInterval(getClock,1000);