꾸매코딩

[JavaScript] 타이머 만들기 [배치 / 초기화버튼 / 마무리] 본문

Project/Timer Project

[JavaScript] 타이머 만들기 [배치 / 초기화버튼 / 마무리]

꾸매코더 2021. 5. 31. 11:50
반응형

결과

스타일 변경

초기화 기능을 추가

결과

Font Awesome

아이콘은 fontawesome 위 사이트를 통해 받아왔다. 

 


코드

[ timer.html ]

<!DOCTYPE html>
<html lang="en">
  <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="css/style.css" />
    <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.15.3/css/all.css"
      integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk"
      crossorigin="anonymous"
    />
    <title>Study Timer</title>
  </head>
  <body>
    <div class="timer-box">
      <div class="timer js-timer">00:00:00</div>
      <div class="timer-form__Btn">
        <div class="timerBtn js-timer__startBtn">
          <i class="fas fa-play"></i>
        </div>
        <div class="timerBtn js-timer__stopBtn">
          <i class="fas fa-pause"></i>
        </div>
        <div class="timerBtn js-timer__resetBtn">
          <i class="fas fa-stop"></i>
        </div>
      </div>
    </div>

    <script src="js/timer.js"></script>
  </body>
</html>

 

 

[ style.css ]

간단한 배치와, 크기만 설정해 주었다.
.timer-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 125px;
}

.timer {
  font-size: 60px;
  margin-bottom: 30px;
  transition: all 1s;
}
.timer-form__Btn {
  display: flex;
  flex-direction: row;
  justify-content: center;
  text-align: center;
  padding-bottom: 25px;
}
.timerBtn {
  height: 30px;
  width: 100px;
  border-radius: 25px;
  margin-left: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.timerBtn i {
  font-size: 22px;
  transition: all 0.2s ease-in-out;
  cursor: pointer;
}
.timerBtn i:active,
.timerBtn i:hover {
  font-size: 30px;
}

.start {
  margin: 50px;
  bottom: 30px;
  font-size: 100px;
  margin: 0px;
  padding: 0px;
}

.start + div > .timerBtn:nth-child(1) {
  display: none;
}

 

 

[timer.js]

const timer = document.querySelector('.js-timer'),
  stopBtn = document.querySelector('.js-timer__stopBtn'),
  startBtn = document.querySelector('.js-timer__startBtn'),
  resetBtn = document.querySelector('.js-timer__resetBtn');

let TIME = 0;
let cron;

function startButton() {
  updateTimer();
  stopButton();
  cron = setInterval(updateTimer, 1000);
  timer.classList.add('start');
}

function stopButton() {
  clearInterval(cron);
  timer.classList.remove('start');
}

function resetButton() {
  timer.innerText = `00:00:00`;
  stopButton();
  timer.classList.remove('start');
  return (TIME = 0);
}

function updateTimer() {
  const hours = Math.floor(TIME / 3600);
  const checkMinutes = Math.floor(TIME / 60);
  const seconds = TIME % 60;
  const minutes = checkMinutes % 60;

  timer.innerText = `${hours < 10 ? `0${hours}` : hours}:${
    minutes < 10 ? `0${minutes}` : minutes
  }:${seconds < 10 ? `0${seconds}` : seconds} `;
  TIME++;
  console.log(TIME - 1);
}

function init() {
  startBtn.addEventListener('click', startButton);
  stopBtn.addEventListener('click', stopButton);
  resetBtn.addEventListener('click', resetButton);
}
init();

계획

타이머의 기능은 완료 되었다고 생각한다. 

좀 더 기능적인 내용은 React로 구현해볼 예정이다.

 

반응형