๐ŸŽจ ํ˜„์žฌ์‹œ๊ฐ„ ํ‘œ์‹œ

11722 ๋‹จ์–ด js์‹ค์Šตjs์‹ค์Šต

https://kirin059.github.io/my_website/

๐Ÿธ html

   <div class="clock-location"> 
       <div class="main-clock"></div>  
   </div>
๐Ÿธ css

/* ์‹œ๊ณ„๊ฐ€ ํ‘œ์‹œ๋˜๋Š” ๊ณต๊ฐ„ ์„ค์ • */
.clock-location{
    width:100%;
    height:160px;
    margin: auto;
    background-color:var( --text-color);
    text-align: center;
    vertical-align: center;
    font-size: 100px;
    font-weight: var( --weight-bold);
}
/* ์‹œ๊ณ„ ํ‘œ์‹œ(00:00:00) */
h1 {
    font-size: var(--font-large);
    font-weight: var(--weight-bold);
    color:var(--point-color);
    text-align: center;
}
๐Ÿธ javascript

 const clock=document.querySelector('.main-clock');

    function getTime() {
      const time = new Date();
      const minutes = time.getMinutes();
      const hours = time.getHours();
      const seconds = time.getSeconds();
      //clock.innerHTML = hour + ":" + minutes + ":" + seconds;
      clock.innerHTML = `${    /* ์‹œ๊ฐ„์ด ๋‘์ž๋ฆฌ๋กœ ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” ๋ช…๋ น๋ฌธ */
        hours <10 ? `0${hours}` : hours
        }
        :${    
        minutes < 10 ? `0${minutes}` : minutes
        }
        :${
        seconds < 10 ? `0${seconds}` : seconds
        }`
    }

    function init(){
        getTime();
        setInterval(getTime, 1000);   /* 1์ดˆ๋งˆ๋‹ค ์‹œ๊ณ„ ์—…๋ฐ์ดํŠธ */
    }

    init();

/* 
`${์ง€์ •๋œ ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ}`

ex)
function color(red, blue){
  return ( `I like ${red} and ${blue}`)
}

document.write(color("peru", "purple"));   // I like peru and purple
=> ์œ„์™€ ๊ฐ™์ด ์ธ์ž x,y๋ฅผ ์ง€์ •ํ•ด์ค€ ๋’ค ํ•ด๋‹น ๊ฐ’์„ ๋ฆฌํ„ดํ•  ๋•Œ, ${๋ฆฌํ„ด์ธ์ž}๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
ES6 ๋ฒ„์ „๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด๋กœ, ๋ฐฑ ์ฟผํŠธ(``)๋กœ ๋ฌถ๊ณ  ๋ณ€์ˆ˜๋Š” ${} ์‚ฌ์ด์— ํ‘œ์‹œํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.
*/

ex)
(1) document.querySelector("#showResult").innerHTML = "๊ฒฐ๊ณผ๋Š” " + result + "์ž…๋‹ˆ๋‹ค";
//showResult ๋ผ๋Š” id ์˜ ๊ณต๊ฐ„ ๋‚ด์šฉ ์•ˆ์—(inner) ์žˆ๋Š” ๊ฒƒ์„ ์ŠคํŠธ๋ง๊ณผ result๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค
(2) document.querySelector("#showResult").innerHTML = `๊ฒฐ๊ณผ๋Š” ${result} ์ž…๋‹ˆ๋‹ค`;
//๋ฐฑ ์ฟผํŠธ(``)๋กœ ๋ฌถ๊ณ  ๋ณ€์ˆ˜๋Š” ${} ์‚ฌ์ด์— ํ‘œ์‹œํ•˜์—ฌ ์ถœ๋ ฅ


/*
? ๋Š” if ์™€ ๊ฐ™์€๊ฐœ๋…,  : ๋Š” or ๊ณผ ๊ฐ™์€๊ฐœ๋…
${ hour < 10 ? `0${hour}` : hours }
hour๊ฐ€ 10๋ณด๋‹ค ์ž‘์œผ๋ฉด(if) `0์„ hour์•ž์— ๋ถ™์—ฌ์ฃผ๊ณ ` ๊ทธ๋ ‡์ง€์•Š์œผ๋ฉด(:) ๊ทธ๋ƒฅ hour๋ฅผ ์จ์ค€๋‹ค๋Š” ๋œป 
*/

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ