forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution for Project 29 - Countdown Timer
- Loading branch information
1 parent
879ac6d
commit 59a3caf
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
let countdown; | ||
const timerDisplay = document.querySelector('.display__time-left'); | ||
const endTime = document.querySelector('.display__end-time'); | ||
const buttons = document.querySelectorAll('[data-time'); | ||
|
||
function timer(secs) { | ||
// clear any existing timers | ||
clearInterval(countdown); | ||
|
||
// current time and time to count down to | ||
const now = Date.now(); | ||
const then = now + secs * 1000; | ||
|
||
// display time left + end time immediately | ||
displayTimeLeft(secs); | ||
displayEndTime(then); | ||
|
||
countdown = setInterval(() => { | ||
const secsLeft = Math.round((then - Date.now()) / 1000); | ||
|
||
// check if we should stop the clock! | ||
if (secsLeft <= 0){ | ||
clearInterval(countdown); | ||
return; | ||
}; | ||
|
||
displayTimeLeft(secsLeft); // display countdown | ||
}, 1000); | ||
}; | ||
|
||
function displayTimeLeft(secs){ | ||
// convert timer into hrs, mins, secs | ||
const hrs = Math.floor(secs / 3600); | ||
const remainderMins = Math.floor((secs % 3600) /60) | ||
const remainderSecs = Math.floor(secs - (hrs*3600) - (remainderMins*60)); | ||
|
||
const display = `${hrs}: ${remainderMins < 10 ? '0' : ''}${remainderMins}: ${remainderSecs < 10 ? '0' : ''}${remainderSecs}`; | ||
|
||
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 mins = end.getMinutes(); | ||
const adjustedMins = mins < 10 ? ('0' + mins) : mins | ||
|
||
endTime.textContent = `Be Back At ${adjustedHour}:${adjustedMins}`; | ||
}; | ||
|
||
function startTimer(){ | ||
const secs = parseInt(this.dataset.time); | ||
timer(secs); | ||
}; | ||
|
||
buttons.forEach(btn => btn.addEventListener('click', startTimer)); | ||
|
||
document.customForm.addEventListener('submit', function(e){ | ||
e.preventDefault(); | ||
|
||
const mins = this.minutes.value; | ||
timer(mins * 60); | ||
|
||
this.reset(); | ||
}) |