-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.js
More file actions
52 lines (45 loc) · 1012 Bytes
/
Timer.js
File metadata and controls
52 lines (45 loc) · 1012 Bytes
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
const worker = new Worker('worker.js')
class Timer {
constructor() {
this.min = 0
this.sec = 0
this.msec = 0
}
start() {
worker.postMessage("start")
worker.onmessage = (e) => {
let [min, sec, msec] = e.data
this.min = min
this.sec = sec
this.msec = msec
this.render()
}
}
stop() {
worker.postMessage("stop")
}
reset() {
this.min = 0
this.sec = 0
this.msec = 0
this.render()
}
render() {
const minSpan = document.getElementById("min")
const secSpan = document.getElementById("sec")
const msecSpan = document.getElementById("msec")
minSpan.innerText = plusMinusZero(this.min)
secSpan.innerText = plusMinusZero(this.sec)
msecSpan.innerText = plusMinusZero(this.msec)
}
}
const plusMinusZero = (num) => {
if (String(num).length === 1) {
return "0" + String(num)
}
if (String(num).length === 3) {
return String(num).substr(0, 2)
}
return String(num)
}
export default Timer