-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_oriented.js
More file actions
98 lines (84 loc) · 2.65 KB
/
Copy pathobject_oriented.js
File metadata and controls
98 lines (84 loc) · 2.65 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* 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");
// Main object constructor
class StopWatch {
constructor(hourEl, minuteEl, secondEl) {
this.hourEl = hourEl;
this.minuteEl = minuteEl;
this.secondEl = secondEl;
this.globalHour = 0;
this.globalMinute = 0;
this.globalSecond = 0;
this.isStart = false;
this.intervalId = null;
}
startTimer() {
if (!this.isStart) {
this.isStart = true;
this.intervalId = setInterval(() => {
this.globalSecond++;
let timeRecords = this.getTimer(this.globalSecond);
this.updateDisplay.call(this, timeRecords);
}, 1000);
}
}
pauseTimer() {
if (this.isStart) {
this.isStart = false;
clearInterval(this.intervalId);
}
}
resetTimer() {
this.pauseTimer();
this.globalHour = 0;
this.globalMinute = 0;
this.globalSecond = 0;
this.hourEl.textContent = `00`;
this.minuteEl.textContent = `00`;
this.secondEl.textContent = `00`;
}
getTimer(sec) {
let minute = parseInt(sec / 60);
let hour = parseInt(minute / 60);
let second = parseInt(sec % 60);
return {
hour,
minute,
second,
};
}
updateDisplay(timeRecords) {
this.hourEl.textContent =
timeRecords.hour < 10 ? `0${timeRecords.hour}` : timeRecords.hour;
this.minuteEl.textContent =
timeRecords.minute < 10
? `0${timeRecords.minute}`
: timeRecords.minute;
this.secondEl.textContent =
timeRecords.second < 10
? `0${timeRecords.second}`
: timeRecords.second;
}
}
const stopwatch_1 = new StopWatch(hourEl, minEl, secondEl);
// call events
btnStartEl.addEventListener("click", stopwatch_1.startTimer.bind(stopwatch_1));
btnPauseEl.addEventListener("click", stopwatch_1.pauseTimer.bind(stopwatch_1));
btnResetEl.addEventListener("click", stopwatch_1.resetTimer.bind(stopwatch_1));