[JavaScript30] ⏲ 29. Countdown Timer
27850 ワード
⏲ 29. Countdown Timer
イニシャルコード<!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">
<title>Countdown Timer</title>
<link href='https://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style_JuneHyung.css">
</head>
<body>
<div class="timer">
<div class="timer__controls">
<button data-time="20" class="timer__button">20 Secs</button>
<button data-time="300" class="timer__button">Work 5</button>
<button data-time="900" class="timer__button">Quick 15</button>
<button data-time="1200" class="timer__button">Snack 20</button>
<button data-time="3600" class="timer__button">Lunch Break</button>
<form name="customForm" id="custom">
<input type="text" name="minutes" placeholder="Enter Minutes">
</form>
</div>
<div class="display">
<h1 class="display__time-left"></h1>
<p class="display__end-time"></p>
</div>
</div>
<script src="scripts_JuneHyung.js"></script>
</body>
</html>
初期画面
🌏 プロセス
👉 1. Timer()
function timer(seconds) {
const now = Date.now();
const then = now + seconds * 1000;
// console.log({ now, then });
displayTimeLeft(seconds);
setInterval(() => {
const secondsLeft = Math.round((then - Date.now()) / 1000);
//check if we should stop it!
if (secondsLeft <= 0) {
clearInterval(countdown);
return;
}
// display it
displayTimeLeft(secondsLeft);
}, 1000);
}
function displayTimeLeft(seconds) {
console.log(seconds);
}
その後、
<!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">
<title>Countdown Timer</title>
<link href='https://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style_JuneHyung.css">
</head>
<body>
<div class="timer">
<div class="timer__controls">
<button data-time="20" class="timer__button">20 Secs</button>
<button data-time="300" class="timer__button">Work 5</button>
<button data-time="900" class="timer__button">Quick 15</button>
<button data-time="1200" class="timer__button">Snack 20</button>
<button data-time="3600" class="timer__button">Lunch Break</button>
<form name="customForm" id="custom">
<input type="text" name="minutes" placeholder="Enter Minutes">
</form>
</div>
<div class="display">
<h1 class="display__time-left"></h1>
<p class="display__end-time"></p>
</div>
</div>
<script src="scripts_JuneHyung.js"></script>
</body>
</html>
function timer(seconds) {
const now = Date.now();
const then = now + seconds * 1000;
// console.log({ now, then });
displayTimeLeft(seconds);
setInterval(() => {
const secondsLeft = Math.round((then - Date.now()) / 1000);
//check if we should stop it!
if (secondsLeft <= 0) {
clearInterval(countdown);
return;
}
// display it
displayTimeLeft(secondsLeft);
}, 1000);
}
function displayTimeLeft(seconds) {
console.log(seconds);
}
👉 2. displayTimeLeft()
残りの時間と秒を見せて
秒を受け入れて適切な計算を行い、10未満の場合は0を加算して出力します.
ex) 01
タブのタイトル(document.title)も、残り時間を表示するように設定されています.
function displayTimeLeft(seconds) {
const minutes = Math.floor(seconds / 60);
const remainderSeconds = seconds % 60;
const display = `${minutes}: ${remainderSeconds < 10 ? '0' : ''}${remainderSeconds}`;
document.title = display;
timerDisplay.textContent = display;
// console.log(minutes);
}
👉 3. displayEndTime()
function displayEndTime(timestamp) {
const end = new Date(timestamp);
const hour = end.getHours();
const adjustedHour = hour > 12 ? hour - 12 : hour;
const minutes = end.getMinutes();
endTime.textContent = `Be Back At ${adjustedHour} : ${minutes < 10 ? '0' : ''}${minutes}`
}
👉 4. quickStart()
メニューバーをクリックして自動的に入力
データセットの値を受信して設定し、ボタンイベントを登録します.
const buttons = document.querySelectorAll('[data-time]');
function timer(seconds) {
// clear any existing timer
clearInterval(countdown);
~~ 중략 ~~
}
function startTimer() {
// console.log(this.dataset.time);
const seconds = parseInt(this.dataset.time);
timer(seconds);
}
buttons.forEach(button => button.addEventListener('click', startTimer));
👉 5.入力時間
document.customForm.addEventListener('submit', function(e) {
e.preventDefault();
const mins = this.minutes.value;
console.log(mins);
timer(mins * 60);
this.reset();
});
Reference
この問題について([JavaScript30] ⏲ 29. Countdown Timer), 我々は、より多くの情報をここで見つけました https://velog.io/@cjh951114/JavaScript30-29.-Countdown-Timerテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol