ํ์ต์ฃผ์ : clock๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel = "stylesheet" href="css/app.css">
<title>Momentum</title>
</head>
<body>
<form class="hidden" id="login-form">
<input type="text" />
<button id="button">Log In</button>
<a href="https://nomadcoders.co/" class="hidden">Go nomad!</a>
</form>
<h2 id ="clock">00:00:00</h2>
<h1 id="greeting" class="hidden"></h1>
<script src="js/clock.js"></script>
<script src="js/greeting.js"></script>
</body>
</html>
const clock = document.querySelector("h2#clock")
//interval : ๋งค๋ฒ ์ผ์ด๋์ผ ํ๋ ๋ฌด์ธ๊ฐ๋ฅผ ๋งํ๋ค.
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");}
/*padStart() : ์ํ๋ ๋ฐฉ์์ผ๋ก ์ถ๋ ฅ๋ ์ ์๋๋ก ์์ชฝ์ ๋ฌธ์์ด์ ์ถ๊ฐํ๋ค. <->padEnd()
"1",padStart(2,"0") : ๋ ์๋ฆฌ์๊ฐ ์๋๊ฒฝ์ฐ์ ์์ '0'์ ์ถ๊ฐํ๋ค.
String(num) : number -> string์ผ๋ก ๋ฐ๊ฟ์ค
clock.innerText = `${hours}:${minutes}:${seconds}`;
date์ ๊ธฐ๋ฅ
1)new Date() : //Mon Jun 12 2023 18:03:10 ํ์ฌ ์๊ฐ๊ณผ ๋ ์ง๋ฅผ ๋ํ๋
2)date.getHours :ํ์ฌ์๊ฐ
3)date.getMinutes :๋ถ
4)date.getSeconds:์ด
2+3+4๋ฅผ ํด์ ๋งค์ด๋ง๋ค ์๊ฐ์ ๋ํ๋ด๋ Date object๋ฅผ ์์ฑํ ์ ์๋ค.*/
getClock();
//์น์ฌ์ดํธ๊ฐ ์คํ๋์๋ง์ ๋ฐ๋ก ์๊ณ๊ธฐ๋ฅ์ด ๊ฐ๋ฅํ๋๋ก ํธ์ถํ๋ค.
setInterval(getClock, 1000);
//1์ด๋ง๋ค ํธ์ถ์ด ์งํ๋๋ค.