-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
60 lines (49 loc) · 1.71 KB
/
main.js
File metadata and controls
60 lines (49 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Identify elements in the DOM
// Text for minutes and seconds to be updated
let minutesText = document.getElementById('minutes');
let secondsText = document.getElementById('seconds');
// Start and reset buttons
const startButton = document.querySelector('#startBtn');
const resetButton = document.querySelector('#resetBtn');
// Watch buttons for a click
startButton.addEventListener('click', displayCountdown);
resetButton.addEventListener('click', resetTimer);
// Set starting Values
let displayMinutes = 25;
let displaySeconds = 0;
let nIntervId;
let minutesStr = '';
let secondsStr = '';
// Function that will refresh the text displayed on the DOM
function updateText() {
minutesText.textContent = (displayMinutes >= 10 ? displayMinutes.toString() : '0' + displayMinutes.toString());
secondsText.textContent = (displaySeconds >= 10 ? displaySeconds.toString() : '0' + displaySeconds.toString());
}
// Count down the minutes and seconds
function refreshDisplay() {
if(displaySeconds === 0 && displayMinutes === 0) {
alert('Times Up!');
clearInterval(nIntervId);
} else if (displayMinutes >= 0 && displaySeconds > 0) {
displaySeconds--;
updateText();
} else if (displayMinutes > 0 && displaySeconds === 0) {
displaySeconds = 59;
displayMinutes--;
updateText();
}
}
// Function that starts the countdown
function displayCountdown() {
//set interval to run every second while theres still time on the clock
nIntervId = setInterval(refreshDisplay, 1000);
}
// Function that resets the timer
function resetTimer() {
clearInterval(nIntervId);
displayMinutes = 25;
displaySeconds = 0;
updateText();
}
//Set starting text
updateText();