-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.ts
More file actions
78 lines (66 loc) · 1.72 KB
/
timer.ts
File metadata and controls
78 lines (66 loc) · 1.72 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
import { Util } from "./util.js";
export class Timer
{
m_IDDuration: string;
m_StartTimestamp: number | null;
m_Ticker: number | null;
constructor(idDuration: string)
{
this.m_IDDuration = idDuration;
this.m_StartTimestamp = null;
this.m_Ticker = null;
}
formatDuration(ms: number): string
{
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}
updateDisplay(): void
{
const elapsedMs = this.m_StartTimestamp === null
? 0
: Date.now() - this.m_StartTimestamp;
Util.setText(this.m_IDDuration, `Duration: ${this.formatDuration(elapsedMs)}`);
}
startTicker(): void
{
if (this.m_Ticker !== null)
{
window.clearInterval(this.m_Ticker);
}
this.m_Ticker = window.setInterval(() =>
{
this.updateDisplay();
}, 1000);
}
reset(): void
{
if (this.m_Ticker !== null)
{
window.clearInterval(this.m_Ticker);
this.m_Ticker = null;
}
this.m_StartTimestamp = null;
this.updateDisplay();
}
startCounting(): void
{
if (this.m_StartTimestamp !== null)
{
return;
}
this.m_StartTimestamp = Date.now();
this.updateDisplay();
}
stop(): void
{
if (this.m_Ticker !== null)
{
window.clearInterval(this.m_Ticker);
this.m_Ticker = null;
}
this.updateDisplay();
}
}