-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (88 loc) · 2.65 KB
/
script.js
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
99
100
101
102
103
104
105
106
// before recording new time check if it is already in the recorded time
const h = document.querySelector('#hour');
const m = document.querySelector('#minute');
const s = document.querySelector('#second');
const c = document.querySelector("#counter");
const startStopBtn = document.querySelector("#startStop");
const recordBtn = document.querySelector("#record");
const resetBtn = document.querySelector("#reset");
const timeRecords = document.querySelector("#timeRecords");
const viewRecord = document.querySelector("#viewRecord");
const recordingPane = document.querySelector("#recordings");
var countTime = 0;
var sec = 0;
var min = 0;
var hrs = 0;
var state = "Start";
startStopBtn.addEventListener('click', timerbutton);
resetBtn.addEventListener('click', function () {
countTime = 0;
sec = 0;
min = 0;
hrs = 0;
display();
state = "Start";
startStopBtn.textContent = state;
timeRecords.innerHTML="";
});
recordBtn.addEventListener("click", function(){
if(hrs!=0||min!=0||sec!=0||countTime!=0){
const newRecord = document.createElement("div");
newRecord.textContent= formatTime(hrs) + ":" + formatTime(min) + ":" + formatTime(sec) + "." + formatTime(countTime);
timeRecords.appendChild(newRecord);
}
});
viewRecord.addEventListener('click', function(){
recordingPane.style.visibility = "visible";
recordingPane.style.width = (document.querySelector("main").offsetWidth + 20) + "px";
window.addEventListener("mouseup",exitRecordingPane);
});
//Start Stop Functions
function timerbutton() {
state = (state === "Start" ? "Stop" : "Start");
startStopBtn.textContent = state;
timer();
}
//function to keep timer rumming
function timer() {
if (state === "Stop") {
increaseTime();
display();
setTimeout(timer, 10);
}
}
function increaseTime() {
countTime++;
if (countTime == 100) {
sec++;
countTime = 0;
}
//minute detector
if (sec == 60) {
min++;
sec = 0;
}
//hour detector
if (min == 60) {
hrs++;
min = 0;
}
}
//display timer time
function display() {
c.textContent = formatTime(countTime);
s.textContent = formatTime(sec);
m.textContent = formatTime(min);
h.textContent = formatTime(hrs);
}
//for showing two digits in output time
function formatTime(value) {
return (value < 10) ? "0" + value : value;
}
//close recordpane
function exitRecordingPane(event){
if(event.target != recordingPane && event.target.parentNode != recordingPane){
recordingPane.style.visibility = 'hidden';
window.removeEventListener("mouseup", exitRecordingPane);
}
}