๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

Interval

by ํ›„์•ผ- 2024. 1. 31.

๐Ÿ’ก ๊ธ‰ํ•˜๊ฒŒ ํ•˜์ง€ ๋ง๊ณ  ํ•˜๋‚˜์”ฉ ํ๋ฆ„์„ ํŒŒ์•…ํ•˜๊ธฐโ—โ—

๐Ÿ“– 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);