ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JavaScript/VanillaJS] ์กฐ์šฉํ•œ ์•Œ๋žŒ(silence alarm) ์›น ์•ฑ ๋งŒ๋“ค๊ธฐ - 5
    javascript 2019. 4. 27. 19:03

     

    ์•ˆ๋…•ํ•˜์„ธ์š”, Jerry์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์–ด๋Š์ƒˆ 5ํŽธ๊นŒ์ง€ ์™”์–ด์š”. ์ด์ œ ๋งˆ๋ฌด๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๋‹ˆ ์ „ํŽธ ์†Œ๊ฐœ๋ฅผ ์ •์„ฑ ๋“ค์—ฌ ์ด์˜๊ฒŒ ํ•ด ๋ณผ๊นŒ ํ•ฉ๋‹ˆ๋‹ค :D

     

    [#1] querySelector( )์™€ ๋ฐฑํ‹ฑ( ` )์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

    โ–ถ https://bejerry.tistory.com/15

     

    [#2] setInterval( )๊ณผ ES6 ๋ง›๋ณด๊ธฐ!

    โ–ถ https://bejerry.tistory.com/16

     

    [#3] typeof( )์™€ ๋ธ”๋ก์Šค์ฝ”ํ”„!

    โ–ถ https://bejerry.tistory.com/17

     

    [#4] classList ๊ฐ์ฒด์™€ css ์• ๋‹ˆ๋ฉ”์ด์…˜ :)

    โ–ถ https://bejerry.tistory.com/18

     


    ์ž, ์ „ ์‹œ๊ฐ„์— ์ถ”๊ฐ€ํ–ˆ๋˜ CSS, ๊ธฐ์–ต๋‚˜์‹œ๋‚˜์š”? ์ถ”๊ฐ€ํ•œ ๋ถ€๋ถ„์„ ๊ธฐ์กด alarm.css์—์„œ ๊ฑท์–ด๋‚ด๊ณ , ์ƒˆ๋กœ์šด popup.css ํŒŒ์ผ๋กœ ๋งŒ๋“ค ๊ฒ๋‹ˆ๋‹ค.

    ์ƒˆ ์ฐฝ์„ ๋„์šฐ๊ธฐ๋กœ ํ–ˆ์—ˆ์ฃ ? ๋„์›Œ์งˆ ์ƒˆ ์ฐฝ์„ HTML ํŒŒ์ผ(popup.html)๊ณผ CSS(popup.css)๋กœ ๋ฏธ๋ฆฌ ์ž‘์—…์„ ํ•ด๋‘๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

     

    4ํŽธ์—์„œ ์ž‘์„ฑํ–ˆ๋˜ css์™€๋Š” ๋‚ด์šฉ์ด ์กฐ๊ธˆ ๋‹ฌ๋ฆฌ์ง€๋‹ˆ ๊ธ€์„ ๊ผผ๊ผผํžˆ ์ฝ์–ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

    (ํ˜ผ๋ž€์ด ์˜ค์…จ๋‹ค๋ฉด ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ช… ์ด ํฌ์ŠคํŒ…์„ ๋‹ค ๋ณด๊ณ  ๋‚˜์‹  ํ›„์—” ์ดํ•ดํ•˜์‹ค ๊ฒ๋‹ˆ๋‹ค!)

     

    popup.css
    
    body {
      width: 100%;
      height: 100%;
    }
    
    .alarmOn {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      animation: blink 1s step-end infinite;
    }
    
    /* for Chrome, Safari */
    @-webkit-keyframes blink {
      0% {background-color: red;}
      50% {background-color: white;}
    }

    4ํŽธ์˜ alarm.css์˜ ์ฝ”๋“œ๋ž‘์€ ์กฐ๊ธˆ ๋‹ค๋ฅด์ฃ ? ๊ทธ ์ด์œ ๋Š” popup.html์˜ body ์•ˆ์—๋Š” div๊ฐ€ ํ•˜๋‚˜๋ฐ–์— ์—†์–ด์„œ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 4ํŽธ์˜ ์†Œ์Šค์™€ ์กฐ๊ธˆ ๋‹ค๋ฅผ ๊ฑฐ์˜ˆ์š”. ๊ทธ๋Ÿผ popup.html ์†Œ์Šค๋ฅผ ํ•œ ๋ฒˆ ๋ณด์‹ค๊นŒ์š”? ์•„๋ž˜์— ์žˆ์Šต๋‹ˆ๋‹ค.

     

    popup.html
    
    <html>
    <head>
    <meta charset='utf-8'>
    <title>POPUP</title>
    <link rel='stylesheet' href='popup.css'>
    </head>
    <body>
      <div class='alarmOn'></div>
    </body>
    </html>

    4ํŽธ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ popup.html์—์„  alarmOn์„ ์•„์˜ˆ ๊ณ ์ •์‹œ์ผœ๋’€์Šต๋‹ˆ๋‹ค. ์™œ๋ƒ? 4ํŽธ์—์„  ํ•œ ํŽ˜์ด์ง€(html)์—์„œ ์‹œ๊ฐ„์ด ๊ฐ™์œผ๋ฉด ๊นœ๋นก์ด๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊นœ๋นก์ด์ง€ ์•Š๊ฒŒ ํ–ˆ์—ˆ์ฃ ? 5ํŽธ์ด 4ํŽธ๊ณผ ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ ์ด๊ฒƒ์ž…๋‹ˆ๋‹ค!

     

    ์ด ํŒ์—…์ฐฝ์ด ๋‚˜์˜จ๋‹ค๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ์‹œ๊ฐ„์ด ๊ฐ™์„ ๋•Œ ๋‚˜์˜ค๋Š”๊ฑฐ์ฃ . (๋ฐฐ๊ฒฝ์ƒ‰์ด ๊นœ๋นก์ด๋Š”)ํŒ์—…์ฐฝ์ด ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์„ค์ • ์‹œ๊ฐ„์ด ๊ฐ™์„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋‹ค๊ฐ€ ๊ฐ™์•„์ง€๋ฉด (๊นœ๋นก์ด๊ณ  ์žˆ๋Š”)ํŒ์—…์ฐฝ์ด ์งœ์ž”~ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฑฐ์ฃ !

     

    popup.html์—” alarm.js๋ฅผ import(?)ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. popup.html์€ js์—†์ด ๊นœ๋นก์ด๋Š” css๋งŒ ๋“ค๊ณ  ์žˆ๋Š” html ํŒŒ์ผ์ด๋‹ˆ๊นŒ์š”!

     

    window.open( ), clearInterval( )
    
    if(current === setValue){
      window.open('popup.html');
      clearInterval(alarm);
      //alarmContainer.classList.add('alarmOn');
    } /*
    else {
      alarmContainer.classList.remove('alarmOn');
    } */

    if( ) ๋ฌธ์˜ ์กฐ๊ฑด์ด true๋ผ๋ฉด if๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•˜๊ณ , else๋Š” ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  classList๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. classList๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ์ด์œ ๋Š” alarmOn์ด๋ผ๋Š” class๋ช…์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•จ์ด์—ˆ์–ด์š”. ๊ณ ์ •์„ ํ•ด๋‘” ์ง€๊ธˆ ์ƒํ™ฉ์ด๋ผ๋ฉด classList๋ฅผ ๋นผ์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ถ”๊ฐ€๋œ ๊ฒƒ์€? window.open( )๊ณผ clearInterval( )์ž…๋‹ˆ๋‹ค.

     

    setInterval( )์„ ์“ฐ๋ฉด (ํ•„์š”์— ๋”ฐ๋ผ) ๋ฐ˜๋ณต์„ ๋๋‚ผ ๋•Œ์— clearInterval( )์„ ์จ์•ผ ํ•˜๋Š” ์ ์„ ๋ฐฐ์› ์—ˆ์Šต๋‹ˆ๋‹ค. ์–ด๋””์„œ์š”?

    setInterval( )๊ณผ clearInterval( ), ๋ฐ”๋กœ 2ํŽธ(https://bejerry.tistory.com/16)์ด์ฃ ! setInterval( )์„ ๋ณ€์ˆ˜(interval_id)์— ๋‹ด๊ณ  clearInterval(interval_id) ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ทธ ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    MDN์—์„œ ์„ค๋ช…ํ•˜๋Š” clearInterval์ž…๋‹ˆ๋‹ค. ์ฐธ๊ณ ์ž๋ฃŒ : https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval

     

    ์œ„์— ์†Œ์Šค๋ฅผ ๋ณด์„ธ์š”. interval_id ์ž๋ฆฌ์— alarm์ด๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์ฃ ? setInterval( ) ๋ฉ”์„œ๋“œ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด๊ณ  ๊ทธ ๋ณ€์ˆ˜๋ฅผ clearInterval( ) ์ธ์ž ์ž๋ฆฌ์— ๋„ฃ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์˜ต์…˜์€ ์œ„์— ์ฐธ๊ณ ์ž๋ฃŒ๋กœ ์ž‘์„ฑํ•ด๋‘” MDN ๋งํฌ๋กœ ์ ‘์†ํ•˜์—ฌ example์„ ํ™•์ธํ•ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿผ ์ง€๊ธˆ๊นŒ์ง€์˜ ์ „์ฒด ์†Œ์Šค๋ฅผ ํ•œ ๋ฒˆ ๋ณด๊ณ , ๋งˆ์ง€๋ง‰์€ ์˜์ƒ์œผ๋กœ ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    (popup.html, popup.css๋Š” ์œ„์— ์†Œ์Šค๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์‹œ ์˜ฌ๋ฆฌ์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.)

     

    ์ „์ฒด ์†Œ์Šค๋ฅผ ํ•œ ๋ฒˆ ๋ณด์‹ค๊นŒ์š”?

     

    alarm.html
    
    <html>
    <head>
      <meta charset='utf-8'>
      <title>ALARM</title>
      <link rel='stylesheet' href='alarm.css'>
    </head>
    <body>
      <div class='container'>
        <div class='js-alarm nav'>
    	  <h1>00:00:00</h1>
    	  <input type='time' min='00:00' max='23:59'/>
    	</div>
      </div>
    <script src='alarm.js'></script>
    </body>
    </html>

     

     

    alarm.css
    
    body {
      width: 100%;
      height: 100%;
    }
    
    .container {
      position: absolute;
      z-index: 1;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      display: table;
    }
    
    .nav {
      display: table-cell;
      vertical-align: middle;
      width: 100%;
      height: 100%;
    }
    
    .js-alarm{
      width: 100%;
      height: 100%;
      text-align: center;
    }

     

    alarm.js
    
    const alarmContainer = document.querySelector('.js-alarm');
    const currentTime = alarmContainer.querySelector('h1');
    const setTime = alarmContainer.querySelector('input');
    
    function getAlarm(){
      const setValue = setTime.value;
      const date = new Date();
      const hours = date.getHours();
      const minutes = date.getMinutes();
      const current = `${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}` : minutes}`;
    	
      if(current === setValue){
    	window.open('popup.html');
    	clearInterval(alarm);	
      }
    }
    
    function getTime(){
      const date = new Date();
      const hours = date.getHours();
      const minutes = date.getMinutes();
      const seconds = date.getSeconds();
    	
      currentTime.innerText = `${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}` : minutes}:${seconds < 10 ? `0${seconds}` : seconds}`;
    }
    
    function init(){
      getTime();
      setInterval(getTime, 1000);
      alarm = setInterval(getAlarm, 1000);
    }
    
    init();

     

    ๋‹ค์‹œ ์†Œ์Šค๋ฅผ ์ฒœ์ฒœํžˆ ๋ณด์„ธ์š”. ์šฐ๋ฆฌ๊ฐ€ ๋ฌด์—‡์„ ๋ฐฐ์› ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    - setInterval( )

    - clearInterval( )

    - Date ๊ฐ์ฒด๋กœ ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ค๊ธฐ

    ์ด ์™ธ์—๋„ ๋ฐฑ ํ‹ฑ( ` ) ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ์ฃ . ์†Œ์Šค๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ๋ณด์‹œ๋ฉด ์žŠ์ง€ ์•Š์œผ์‹ค๊ฑฐ์—์š”!

    ๋ฐฐ์šด ๊ฒƒ๋“ค์„ ์ด์šฉํ•ด์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด๋ณด๋Š” ๊ฒƒ๋„ ์—„์ฒญ ์ข‹์€ ๊ณต๋ถ€ํšจ๊ณผ ์ค‘ ํ•˜๋‚˜์ฃ .

     

    ๊ทธ๋Ÿผ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ ๊ฒŒ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์˜์ƒ์œผ๋กœ๋„ ๋ณด๊ณ  ๊ฐ€์‹ค๊ฒŒ์š”~

     

     

     

     

    ์˜์ƒ์„ ๋ณด์‹œ๋‹ค๋ณด๋ฉด ์•Œ๋žŒ์†Œ๋ฆฌ๊ฐ€ ๋‚˜๋Š”๋ฐ ๊ทธ๊ฑด ์˜์ƒํŽธ์ง‘ํ•  ๋•Œ์— ์ถ”๊ฐ€๋กœ ๋„ฃ์€ ํšจ๊ณผ ์‚ฌ์šด๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์›น ์•ฑ์ด๋ž‘์€ ๋ฌด๊ด€ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์กฐ์šฉํ•œ ๋””์Šคํ”Œ๋ ˆ์ด ์•Œ๋žŒ๋งŒ ๋งŒ๋“ค์—ˆ์ฃ . ํšŒ์‚ฌ์—์„œ ๋‚˜์˜ ํ‡ด๊ทผ์„ ์•Œ๋ ค์ฃผ๋Š” silence alarm ์ œ์ž‘์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

     

    ์–ด๋– ์…จ๋‚˜์š”? ํ˜น์‹œ ๊ถ๊ธˆํ•˜์‹  ์  ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๊ณ , ๊ฐ•์˜์— ๋ถ€์กฑํ–ˆ๋˜ ์  ํ˜น์€ ํ•„์š”ํ–ˆ๋˜ ๊ฒƒ๋“ค ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๋‹ค๋ฅธ ํฌ์ŠคํŒ…์—์„œ ๊ผญ ์ฐธ๊ณ ํ•˜์—ฌ ๊ธ€ ์“ฐ๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

    ๋Œ“๊ธ€

Posted by Bejerry.