-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelapsedtime.html
More file actions
85 lines (75 loc) · 2.34 KB
/
elapsedtime.html
File metadata and controls
85 lines (75 loc) · 2.34 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
<script>
class Timer {
constructor() {
this.isRunning = false;
this.startTime = 0;
this.overallTime = 0;
}
_getTimeElapsedSinceLastStart() {
if (!this.startTime) {
return 0;
}
return Date.now() - this.startTime;
}
start() {
if (this.isRunning) {
return console.error('Timer is already running');
}
this.isRunning = true;
this.startTime = Date.now();
}
stop() {
if (!this.isRunning) {
return console.error('Timer is already stopped');
}
this.isRunning = false;
this.overallTime = this.overallTime + this._getTimeElapsedSinceLastStart();
}
reset() {
this.overallTime = 0;
if (this.isRunning) {
this.startTime = Date.now();
return;
}
this.startTime = 0;
}
getTime() {
if (!this.startTime) {
return 0;
}
if (this.isRunning) {
return this.overallTime + this._getTimeElapsedSinceLastStart();
}
return this.overallTime;
}
}
function secondsToHms(d) {
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
var hDisplay = h > 0 ? h + (h == 1 ? "h, " : " h, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? "m, " : "m, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? "s" : "s") : "";
return hDisplay + mDisplay + sDisplay;
}
const timer = new Timer();
function startTimer() {
timer.start();
setInterval(() => {
const timeInSeconds = Math.round(timer.getTime() / 1000);
document.getElementById('time').innerText = secondsToHms(timeInSeconds);
}, 100)
}
function stopTimer() {
timer.stop();
}
</script>
<p>
<span id="time" style="padding-right: '5px';"></span>
<button onclick="startTimer()">start</button>
<button onclick="stopTimer()">stop</button>
</p>
<iframe src="https://helpingwithmath.com/generators/5nbt5-multiplication01/" width="100%" height="90%"
title="description ">
</iframe>