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

2021. 5. 31. 11:50·Project/Timer Project
반응형

결과

스타일 변경

초기화 기능을 추가

결과

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로 구현해볼 예정이다.

 

반응형
'Project/Timer Project' 카테고리의 다른 글
  • [React] state를 활용한 동적인 타이머 만들기 - 시작, 정지, 리셋 기능 구현
  • [React] 타이머 만들기 react-compound-timer 라이브러리 활용
  • [React] new Date(), state를 활용한 동적인 현재시간 나타내기
  • [JavaScript] 타이머 만들기 [타이머 기능 구현 / 시작, 정지 버튼]
꾸매코더
꾸매코더
웹 / 앱 개발자
  • 꾸매코더
    꾸매코딩
    꾸매코더
  • 전체
    오늘
    어제
    • 분류 전체보기 (96)
      • JavaScript (18)
        • Deep Dive (0)
      • React (5)
      • React-Native (9)
      • CSS (3)
      • TypeScript (1)
      • CS (10)
      • GitHub (4)
      • Clone Coding (15)
        • Kakaotalk [HTML + CSS] (10)
        • Momentum [JavaScript] (5)
      • Project (30)
        • Timer Project (14)
        • Netflix [React + Hooks] (12)
        • ToDoList [React-Native] (4)
      • 회고 (1)
  • 링크

    • Github
  • hELLO· Designed By정상우.v4.10.3
꾸매코더
[JavaScript] 타이머 만들기 [배치 / 초기화버튼 / 마무리]
상단으로

티스토리툴바