-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional.js
More file actions
80 lines (68 loc) · 1.91 KB
/
Copy pathfunctional.js
File metadata and controls
80 lines (68 loc) · 1.91 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Title: Stopwatch
* Description: stopwatch with three features (start, pause, reset)
* Author: Samin Yasar
* Date: 12/July/2021
*/
/*
* select all necessary DOM
* define all functionality
* call events
*/
// DOM selecting
const displayEl = document.getElementById("display");
const hourEl = document.getElementById("hour");
const minEl = document.getElementById("min");
const secondEl = document.getElementById("second");
const btnStartEl = document.getElementById("btnStart");
const btnPauseEl = document.getElementById("btnPause");
const btnResetEl = document.getElementById("btnReset");
let globalSecond = 0;
let isStart = false;
let intervalId = null;
// functionality define
function startTimer() {
if (!isStart) {
isStart = true;
intervalId = setInterval(() => {
globalSecond++;
let timeRecords = getTimer(globalSecond);
updateDisplay(timeRecords);
}, 1000);
}
}
function pauseTimer() {
if (isStart) {
isStart = false;
clearInterval(intervalId);
}
}
function resetTimer() {
pauseTimer();
globalSecond = 0;
hourEl.textContent = "00";
minEl.textContent = "00";
secondEl.textContent = "00";
}
function getTimer(sec) {
let minute = parseInt(sec / 60);
let hour = parseInt(minute / 60);
let second = parseInt(sec % 60);
return {
hour,
minute,
second,
};
}
function updateDisplay(timeRecords) {
hourEl.textContent =
timeRecords.hour < 10 ? `0${timeRecords.hour}` : timeRecords.hour;
minEl.textContent =
timeRecords.minute < 10 ? `0${timeRecords.minute}` : timeRecords.minute;
secondEl.textContent =
timeRecords.second < 10 ? `0${timeRecords.second}` : timeRecords.second;
}
// call events
btnStartEl.addEventListener("click", startTimer);
btnPauseEl.addEventListener("click", pauseTimer);
btnResetEl.addEventListener("click", resetTimer);