Skip to content

Commit 93dd5d2

Browse files
committed
Including JS from project 29
1 parent dd8ba25 commit 93dd5d2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

29 - Countdown Timer/typescripts.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var countdown;
2+
var timerDisplay = document.querySelector('.display__time-left');
3+
var endTime = document.querySelector('.display__end-time');
4+
var buttons = document.querySelectorAll('[data-time]');
5+
function timer(seconds) {
6+
clearInterval(countdown);
7+
var now = Date.now();
8+
var then = now + seconds * 1000;
9+
displayTimeLeft(seconds);
10+
displayEndTime(then);
11+
countdown = setInterval(function () {
12+
var secondsLeft = Math.round((then - Date.now()) / 1000);
13+
if (secondsLeft < 0) {
14+
clearInterval(countdown);
15+
return;
16+
}
17+
displayTimeLeft(secondsLeft);
18+
}, 1000);
19+
}
20+
function displayTimeLeft(seconds) {
21+
var minutes = Math.floor(seconds / 60);
22+
var remainderSeconds = seconds % 60;
23+
var display = minutes + ":" + (remainderSeconds < 10 ? '0' : '') + remainderSeconds;
24+
document.title = display;
25+
timerDisplay.textContent = display;
26+
}
27+
function displayEndTime(timestamp) {
28+
var end = new Date(timestamp);
29+
var hour = end.getHours();
30+
var adjustedHour = hour > 12 ? hour - 12 : hour;
31+
var minutes = end.getMinutes();
32+
endTime.textContent = "Be back at " + adjustedHour + ":" + (minutes < 10 ? '0' : '') + minutes;
33+
}
34+
function startTimer() {
35+
var seconds = Number(this.dataset.time);
36+
timer(seconds);
37+
}
38+
buttons.forEach(function (button) { return button.addEventListener('click', startTimer); });
39+
document.customForm.addEventListener('submit', function (e) {
40+
e.preventDefault();
41+
var mins = this.minutes.value;
42+
timer(mins * 60);
43+
this.reset();
44+
});

0 commit comments

Comments
 (0)