๐ก ๊ธํ๊ฒ ํ์ง ๋ง๊ณ ํ๋์ฉ ํ๋ฆ์ ํ์ ํ๊ธฐโโ
๐ Interval (์ ํ ์๊ฐ๋ง๋ค ํธ์ถ)
-> setInterval
โพ html code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel="stylesheet" href="./css/styles.css">
</head>
<body>
<h1 class="practice">์๋
ํ์ธ์ ๋ฐ๊ฐ์์ </h1>
<h2 id="clock">00:00:00</h2>
<script src="./js/index.js"></script>
</body>
</html>
โพ js code
const clocked = document.querySelector("#clock");
function speaking() {
console.log("1์ด๋ง๋ค ๋ฐ๋ณตํ๊ธฐ");
}
setInterval(speaking, 1000); // 1000ms 1์ด
โณ ์๊ณ ๋ง๋ค๊ธฐ
โพ js code
const clocked = document.querySelector("#clock");
function setclocked() {
const date = new Date();
console.log(`${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
}
setInterval(setclocked, 1000);
โพ new Date() = ํ ๋ ์ง, ์๊ฐ ๋ฑ ๋ํ๋ด์ค
โพ ` ` = ๋ฐฑํฑ
โพ js code
const clocked = document.querySelector("#clock");
function setclocked() {
const date = new Date();
// innerText ๋ก ํ๋ฉด์ ๊ตฌํํ๊ธฐ
clocked.innerText = (`${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
}
// ํค์๋ง์ ์๊ฐ ๋์ค๊ฒํ๊ธฐ
setclocked();
// ๊ทธ ํ ์ด๋ง๋ค ์๊ฐ ๋์ค๊ฒํ๊ธฐ
setInterval(setclocked, 1000);