[JavaScript] 실시간 타이머(시계) 만들기
■ 실시간 타이머(시계) 만들기
# 소스코드
<html> <head> <title>:: JavaScript 전광판 효과 ::</title> <script type="text/javascript"> document.addEventListener("DOMContentLoaded", function() { // 시간을 딜레이 없이 나타내기위한 선 실행 realTimer(); // 이후 0.5초에 한번씩 시간을 갱신한다. setInterval(realTimer, 500); }); // 시간을 출력 function realTimer() { const nowDate = new Date(); const year = nowDate.getFullYear(); const month= nowDate.getMonth() + 1; const date = nowDate.getDate(); const hour = nowDate.getHours(); const min = nowDate.getMinutes(); const sec = nowDate.getSeconds(); document.getElementById("nowTimes").innerHTML = year + "-" + addzero(month) + "-" + addzero(date) + " " + hour + ":" + addzero(min) + ":" + addzero(sec); } // 1자리수의 숫자인 경우 앞에 0을 붙여준다. function addzero(num) { if(num < 10) { num = "0" + num; } return num; } </script> </head> <body> <h1>■ 현재시간 : <span id="nowTimes"></span></h1> </body> </html> |
# 출력결과