Visitar URL original
fiished countdown timer · soyaine/JavaScript30@6ebbd06 · GitHub
Skip to content

Commit 6ebbd06

Browse files
committed
fiished countdown timer
1 parent c804d56 commit 6ebbd06

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
11
let countDown;
2+
const timerDisplay = document.querySelector('.display__time-left');
3+
const endTime = document.querySelector('.display__end-time');
4+
const buttons = document.querySelectorAll('[data-time]');
25
function timer(seconds) {
6+
clearInterval(countDown);
37
const now = Date.now();
48
const then = now + seconds * 1000;
5-
console.log({now, then});
9+
dispalyTimeLeft(seconds);
10+
displayEndTime(then);
611
countDown = setInterval(() => {
7-
const secondsLeft = Math.round((then - now) / 1000);
12+
const secondsLeft = Math.round((then - Date.now()) / 1000);
813
//check if stop
914
if(secondsLeft < 0) {
1015
clearInterval(countDown);
1116
return;
1217
}
13-
console.log(secondsLeft);
18+
dispalyTimeLeft(secondsLeft);
1419
}, 1000);
15-
}
20+
}
21+
22+
function dispalyTimeLeft(seconds) {
23+
const minutes = Math.floor(seconds / 60);
24+
const displaySeconds = seconds % 60;
25+
const display = `${minutes}:${displaySeconds < 10 ? '0' : ''}${displaySeconds}`;
26+
document.title = display;
27+
timerDisplay.textContent = display;
28+
}
29+
30+
function displayEndTime(timestamp){
31+
const end = new Date(timestamp);
32+
const hour = end.getHours();
33+
const minutes = end.getMinutes();
34+
endTime.textContent = `Be back at ${hour > 12 ? hour - 12: hour}:${minutes < 10 ? '0':''}${minutes}`;
35+
}
36+
37+
function startTimer(){
38+
const seconds = parseInt(this.dataset.time);
39+
timer(seconds);
40+
}
41+
42+
buttons.forEach(button => {
43+
button.addEventListener('click', startTimer);
44+
})
45+
46+
document.customForm.addEventListener('submit', function(e){
47+
e.preventDefault();
48+
const minutes = this.minutes.value;
49+
timer(minutes * 60);
50+
this.reset();
51+
})

0 commit comments

Comments
 (0)