-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
145 lines (112 loc) · 3.73 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// PSEUDO CODE
// We add an event (click) to the radio buttons to indicate which breast is the food canteen at the moment
// Add event(click) to Start and Stop buttons. This event should show the timer and also start/stop the timer.
// The stop button also should capture the feeding duration and display(store) the feeding duration. This will also reset the timer.
// The feeding logs will be a different page
// CHange feeding logs to an icon(like paper/stack of papers)
// Declaring the Variable
const leftFoodBank = document.querySelector(".left");
const rightFoodBank = document.querySelector(".right");
const startBtn = document.querySelector(".start");
const pauseBtn = document.querySelector(".stop");
let foodBankChosen = '';
let feedingLog = []
// check if there is a stored log
//update the log if true
let getStoredLog = JSON.parse(localStorage.getItem("log"))
if(getStoredLog){
feedingLog = getStoredLog
}
const getStopWatchEl = document.querySelector("#watch")
let [hrs, min, sec, millisec] = [0, 0, 0, 0]
let integer = null
// Add event listerner
leftFoodBank.addEventListener("click", () => {
foodBankChosen = 'leftFoodBank'
startBtn.disabled = false
})
rightFoodBank.addEventListener("click", () => {
foodBankChosen = 'rightFoodBank'
startBtn.disabled = false
})
startBtn.addEventListener("click", displayTimer)
function displayTimer() {
if (foodBankChosen !== '') {
startBtn.disabled = false
}
if (integer !== null) {
clearInterval(integer)
}
integer = setInterval(getInterval, 10)
}
const getInterval = () => {
millisec += 10;
if (millisec === 1000) {
millisec = 0;
sec++;
} if (sec === 60) {
sec = 0;
min++;
} if (min === 60) {
min = 0
hrs++
} else if (hrs === 24) {
// console.log("stop")
}
let ms = millisec < 10 ? "00" + millisec : millisec < 100 ? "00" + millisec : millisec
let stringMs = ms.toString().slice(-3, -1)
let h = hrs < 10 ? "0" + hrs : hrs
let s = sec < 10 ? '0' + sec : sec;
let m = min < 10 ? '0' + min : min;
getStopWatchEl.textContent =
`${h}:${m}:${s}:${stringMs}`
}
const getResetBtn = document.querySelector(".reset")
let getTime = new Date()
let date = getTime.toDateString();
let minutes = getTime.getMinutes()
let seconds = getTime.getSeconds()
let hours = getTime.getHours()
getResetBtn.addEventListener("click", () => {
//before the stopwatch is reset get the log
let stopWatchData = getStopWatchEl.textContent;
startBtn.disabled = true
let uid = generateId()
if (stopWatchData !== '00:00:00:00') {
let log = {
uid,
date,
time: `${hours}:${minutes}:${seconds}`,
foodBankChosen,
stopWatchData
};
recentLog()
//push log into the feedinglog
feedingLog.push(log);
console.log(feedingLog);
const localStorageLog = JSON.stringify(feedingLog)
localStorage.setItem('log', localStorageLog)
//clear the stopwatch
clearInterval(integer);
[hrs, min, sec, millisec] = [00, 00, 00, 00];
//reset textContent
getStopWatchEl.textContent = '00:00:00:00';
leftFoodBank.checked = false
rightFoodBank.checked = false
}
})
0
pauseBtn.addEventListener("click", () => {
clearInterval(integer)
})
// create function to display most recent feeding time under the feeding log link
//create function to grab whatever is in the timer textContent and place into feeding logs
//make sure each time you click radio buttons the timer needs to reset to zero
const recentLog = () => {
if (getStopWatchEl.textContent !== '00:00:00:00') {
document.querySelector('.recentlog').textContent = getStopWatchEl.textContent
}
}
//function to generate a unique id
const generateId = () => {
return `${performance.now()}${Math.random().toString().slice(5)}`.replace('.',"a")}