-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (68 loc) · 1.95 KB
/
Copy pathscript.js
File metadata and controls
88 lines (68 loc) · 1.95 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
// ==== STATE ====
let startTime = null;
let elapsed = 0;
let interval = null;
let running = false;
// ====FORMAT TIME ====
function format(ms){
let totalSeconds = Math.floor(ms/1000);
let hours = String(Math.floor(totalSeconds/3600)).padStart(2, '0');
let minutes = String(Math.floor((totalSeconds%3600) / 60)).padStart(2, '0');
let seconds = String(totalSeconds % 60).padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
}
// ====UPDATE DISPLAY====
function update(){
let now = Date.now();
let diff = now - startTime;
document.getElementById("timer").innerText = format(diff);
}
//====START====
function start(){
if(running) return;
startTime = Date.now() - elapsed;
interval = setInterval(update, 1000);
running = true;
}
//====RESET====
function reset(){
clearInterval(interval);
startTime = null;
elapsed = 0;
running = false;
document.getElementById("timer").innerText = "00:00:00";
}
//====PAUSE====
function pause(){
if(!running) return;
elapsed = Date.now() - startTime;
clearInterval(interval);
running = false;
}
//====SAVE SESSION====
function save(){
if(!startTime) return;
let end = Date.now();
let duration = running ? (end - startTime) : elapsed;
let session = {
time: new Date().toLocaleString(),
duration: duration
};
let data = JSON.parse(localStorage.getItem("sessions")) || [];
data.push(session);
localStorage.setItem("sessions",JSON.stringify(data));
showHistory();
}
//====SHOW HISTORY====
function showHistory(){
let data = JSON.parse(localStorage.getItem("sessions")) || [];
let list = document.getElementById("history");
list.innerHTML = "";
data.forEach(element => {
let li = document.createElement("li");
li.innerText=`${element.time} - ${format(element.duration)}`;
list.appendChild(li);
});
}
//====LOAD ON START ====
showHistory();