-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathchronometer.js
More file actions
71 lines (56 loc) · 1.57 KB
/
chronometer.js
File metadata and controls
71 lines (56 loc) · 1.57 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
class Chronometer {
constructor() {
this.currentTime = 0;
this.milliSeconds = 0; //centesimas
this.intervalId = null;
}
start(callback) {
this.intervalId = setInterval(() => {
this.milliSeconds += 1; // cada 10 ms se suma 1 centesima
if (this.milliSeconds === 100) {
this.currentTime += 1;
this.milliSeconds = 0;
}
if (callback) {
callback();
}
}, 10);
}
// mejor hacerlo sin this. no guardas nada extra en el objeto.
getMinutes() {
return Math.floor(this.currentTime / 60);
}
getSeconds() {
return this.currentTime % 60;
}
getMilliseconds() {
return this.milliSeconds;
}
computeTwoDigitNumber(value) {
let valueString = "0" + value
return valueString.slice(-2);
}
stop() {
clearInterval(this.intervalId);
}
reset() {
this.currentTime = 0;
document.getElementById("minDec").innerHTML = "0";
document.getElementById("minUni").innerHTML = "0";
document.getElementById("secDec").innerHTML = "0";
document.getElementById("secUni").innerHTML = "0";
milDecElement.innerHTML = "0";
milUniElement.innerHTML = "0";
}
split() {
const mm = this.computeTwoDigitNumber(this.getMinutes())
const ss = this.computeTwoDigitNumber(this.getSeconds())
const cc = this.computeTwoDigitNumber(this.getMilliseconds())
return `${mm}:${ss}:${cc}`
}
}
// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
module.exports = Chronometer;
}