비전공자 개발일기

Alarm Application 본문

Javascript

Alarm Application

HiroDaegu 2022. 5. 2. 00:12
728x90
SMALL

 

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css" integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm" crossorigin="anonymous"/>
  <title>Alarm App</title>
  <script defer src="main.js"></script>
</head>
<body>
  <nav>
    <h1>
      ALARM APP
      <i class="fas fa-clock"></i>
    </h1>
  </nav>
  <div class="container">
    <p>Enter the Time</p>
    <div class="inputs">
      <input type="number" id="txt" placeholder="" class="time" autocomplete="off">
      <span>:</span>
      <input type="number" id="txt2" placeholder="" class="time" autocomplete="off">
    </div>
    <div class="btns">
      <button id="btn" class="butt">SET</button>
      <button id="btn2" class="butt">STOP</button>
    </div>
    <div class="info">
      <p id="para"></p>
    </div>
  </div>
</body>
</html>
:root {
  --bg-prim: #12181b;
  --bg-sec: #383838;
  --clr: #fff;
}

body {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  background-color: var(--bg-prim);
}

nav {
  text-align: center;
  padding: 0 1rem;
  color: var(--clr);
  background-color: var(--bg-sec);
}

.container {
  margin: 5rem auto;
  background-color: var(--bg-sec);
  padding: 1rem;
  color: var(--clr);
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 40rem;
  border-radius: 10px;
}

span {
  font-size: 1.3rem;
}

.time {
  text-align: center;
  outline: none;
  font-size: 1.5rem;
  height: 3rem;
  margin: 1rem;
  width: 5rem;
}

.butt {
  font-size: 1rem;
  margin-top: .6rem;
  outline: none;
  border: none;
  width: 5rem;
  padding: .55rem;
  cursor: pointer;
  color: #fff;
  border-radius: 4px;
  background-color: hsl(217deg, 99%, 65%);
}

#btn2 {
  visibility: hidden;
  display: block;
}

p {
  font-size: 1.3rem;
}

@media (max-width: 650px) {
  .container {
    width: 20rem;
  }
}
let para = document.getElementById("para");
let text = document.getElementById("txt");
let text2 = document.getElementById("txt2");
let btn = document.getElementById("btn");
let btn2 = document.getElementById("btn2");

window.addEventListener('load', () => {
  text.placeholder = new Date().getHours();
  text2.placeholder = new Date().getMinutes();
})

btn.addEventListener('click', alerm);
let x;
function alerm() {
  if(text.value && text2.value) {
    x = setInterval(() => {
      setAlarm();
    }, 1000)
  } else {
    alert("ENTER THE HRS AND MINS!")
  }
}

function setAlarm() {
  let d = new Date().toLocaleDateString();
  let then = new Date(`${d} ${text.value}: ${text2.value}`).getTime();
  let now = new Date().getTime();

  let distance = then - now;
  let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = Math.floor((distance % (1000 * 60)) / 1000);

  para.innerHTML = `ALARM IN - ${hours} : ${minutes} : ${seconds}`;

  if(distance < 0) {
    clearInterval(x);
    para.innerHTML = `It's ALARM TIME`;
    let audio = new Audio("sound.mp3");
    audio.play();
    btn2.style.visibility = `visible`;
    btn2.addEventListener("click", () => {
      para.innerHTML = ``;
      audio.pause();
      btn2.style.visibility = `hidden`;
      text.value = ``;
      text2.value = ``;
    })
  }
}
728x90
LIST

'Javascript' 카테고리의 다른 글

Animated Submit BUTTON  (0) 2022.05.10
Lock Animation  (0) 2022.05.05
3D Card  (0) 2022.04.27
Circle Loading Tape  (0) 2022.04.24
Age Calculator  (0) 2022.04.21