자바스크립트 예제

객체의 직렬화 ·2023년 7월 1일

FE

목록 보기
7/10
  1. 연도와 달, 날짜까지 표시되는 시계 기능이 추가됐어요. JavaScript
function toPadStart(num){
return String(num).padStart(2,'0')
}

function updateClock() {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const date = now.getDate();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const clock = document.getElementById("clock");
clock.innerHTML = ${year}년 ${toPadStart(month)}월 ${toPadStart(date)}일 ${toPadStart(hours)}시 ${toPadStart(minutes)}분 ${toPadStart(seconds)}초;
// 1초마다 시간을 업데이트합니다.
setTimeout(updateClock, 1000);
}

1. 연도가 표시되는지 확인
2. 달이 표시되는지 확인
3. 날짜가 표시되는지 확인
4. 1초마다 업데이트 되는지 확인

  1. setTimeout을 이용해 setInterval처럼 1초마다 console.log(’내용은 자유’)를 호출
function logMessage() {
  console.log('내용은 자유');
  setTimeout(logMessage, 1000);
}

setTimeout(logMessage, 1000);

1. setTimeout을 이용하였는지 확인
2. 1초마다 반복 호출되는지 확인

profile
Free sprit engineer | 커밋할때마다 손 떨리는 화학원소 - 탄소(C)같은 초급개발자 | James Gosling 같은 사람이 되고 싶은 싶은 자바꿈나무🌱

0개의 댓글