๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/Vanilla JS

[๋ฐ”๋‹๋ผJS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ] Making a JS Clock part Two

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 26.
๋ฐ˜์‘ํ˜•

[์ถœ์ฒ˜-์œ ํŠœ๋ธŒ ๋…ธ๋งˆ๋“œ ์ฝ”๋” Nomad Coders]

https://youtu.be/jstjlCZa7nA

 

setInterval(fn, 1000)

์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ํ•จ์ˆ˜, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์‹คํ–‰ํ•  ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ(millisecond)์ด๋‹ค

 

3์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ 'say hi'๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

 

 

const clockContainer = document.querySelector(".js-clock"),
    clockTitle = clockContainer.querySelector("h1");

function getTime(){
    const date = new Date();
    const minutes = date.getMinutes();
    const hours = date.getHours();
    const seconds = date.getSeconds();
    clockTitle.innerText = `${hours}:${minutes}:${seconds}`;
}

function init(){
    getTime();
    setInterval(getTime, 1000);
}

init();

 

 

 

-์‚ผํ•ญ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•˜๊ธฐ

${seconds < 10 ? `0${seconds}` : seconds}`
const clockContainer = document.querySelector(".js-clock"),
    clockTitle = clockContainer.querySelector("h1");

function getTime(){
    const date = new Date();
    const minutes = date.getMinutes();
    const hours = date.getHours();
    const seconds = date.getSeconds();
    clockTitle.innerText = `${hours}:${minutes}:${seconds < 10 ? `0${seconds}` : seconds}`;
    
}

function init(){
    getTime();
    setInterval(getTime, 1000);
}

init();

์›๋ž˜๋Š” ์ด๋žฌ์—ˆ๋Š”๋ฐ
์ด๋ ‡๊ฒŒ ๋ฐ”๋€œ

 

 

const clockContainer = document.querySelector(".js-clock"),
    clockTitle = clockContainer.querySelector("h1");

function getTime(){
    const date = new Date();
    const minutes = date.getMinutes();
    const hours = date.getHours();
    const seconds = date.getSeconds();
    clockTitle.innerText = `${hours < 10 ? `0${hours}` : hours}:${
        minutes < 10 ? `0${minutes}` : minutes
    }:${seconds < 10 ? `0${seconds}` : seconds}`;
    
}

function init(){
    getTime();
    setInterval(getTime, 1000);
}

init();

 

์‹œ, ๋ถ„์—๋„ ๋˜‘๊ฐ™์ด ์ ์šฉํ•ด์คŒ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€