-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathchronometer.js
More file actions
48 lines (40 loc) · 1.36 KB
/
chronometer.js
File metadata and controls
48 lines (40 loc) · 1.36 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
class Chronometer {
constructor() {
this.currentTime = 0;
this.intervalId = null
}
start(callback) {
this.intervalId = setInterval(() => {// se usa función flecha para referenciar al chronometer
this.currentTime += 1;
if (callback){
callback();
}
}, 1000);
}
getMinutes() { //math floor redondea el valor de la opereción
return Math.floor(this.currentTime /60); // extraer minutos enteros
}
getSeconds() { // calcular segundos restantes de la función anterior
return Math.floor (this.currentTime%60);
}
computeTwoDigitNumber(value) {
return("0" + value).slice(-2) // esta función devuelve 0 + el parametro y el slice lo que hace es que siempre devuelva dos digitos
}
stop() {
clearInterval(this.intervalId);
}
reset() {
this.currentTime = 0;
this.intervalId= null;
}
split() { // crear el diseño mm:ss creando dos contaste donde se llama los minutos y segundo previamente creados. Para combinarlos y devolverlos en forma de cadena
const minutes = this.computeTwoDigitNumber(this.getMinutes());
const seconds = this.computeTwoDigitNumber(this.getSeconds());
return `${minutes}:${seconds}`;
}
}
// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
module.exports = Chronometer;
}