Skip to content

Commit acd21fc

Browse files
committed
29 done
1 parent b4cf5a9 commit acd21fc

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

29 - Countown Timer/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ <h1 class="display__time-left"></h1>
2424
</div>
2525
</div>
2626

27-
<script src="scripts-START.js"></script>
27+
<script src="scripts-RIO.js"></script>
2828
</body>
2929
</html>

29 - Countown Timer/scripts-RIO.js

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

0 commit comments

Comments
 (0)