Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

29 - Countdown Timer #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions 29 - Countdown Timer/changi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
let countdown; // interval
const timerDisplay = document.querySelector(".display__time-left");
const endTime = document.querySelector(".display__end-time");
const buttons = document.querySelectorAll("[data-time]");

function timer(seconds) {
clearInterval(countdown);

const now = Date.now();
const then = now + seconds * 1000;
displayTimeLeft(seconds);
displayEndTime(then);

countdown = setInterval(() => {
const secondsLeft = Math.round((then - Date.now()) / 1000);

if (secondsLeft < 0) {
clearInterval(countdown);
return;
}

displayTimeLeft(secondsLeft);
}, 1000);
}

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;
}

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}`;
}

function startTimer() {
const seconds = parseInt(this.dataset.time);
timer(seconds);
}

buttons.forEach((button) => button.addEventListener("click", startTimer));

// get time from form tag
document.customForm.addEventListener("submit", function (e) {
e.preventDefault();
const mins = this.minutes.value;
console.log(mins);
timer(mins * 60);
this.reset();
});
53 changes: 29 additions & 24 deletions 29 - Countdown Timer/index.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Countdown Timer</title>
<link href='https://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style.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>
<head>
<meta charset="UTF-8" />
<title>Countdown Timer</title>
<link
href="https://fonts.googleapis.com/css?family=Inconsolata"
rel="stylesheet"
type="text/css"
/>
<link rel="stylesheet" href="style.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>
<div class="display">
<h1 class="display__time-left"></h1>
<p class="display__end-time"></p>
</div>
</div>

<script src="scripts-START.js"></script>
</body>
<!-- <script src="scripts-START.js"></script> -->
<script src="changi.js"></script>
</body>
</html>