generated from jesusgpa/2023-2024-CSAAI-Practicas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b57b1f
commit 8c59a27
Showing
3 changed files
with
103 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//-- Clase cronómetro | ||
class Crono { | ||
|
||
//-- Constructor. Hay que indicar el | ||
//-- display donde mostrar el cronómetro | ||
constructor(display) { | ||
this.display = display; | ||
|
||
//-- Tiempo | ||
this.cent = 0, //-- Centésimas | ||
this.seg = 0, //-- Segundos | ||
this.min = 0, //-- Minutos | ||
this.timer = 0; //-- Temporizador asociado | ||
} | ||
|
||
//-- Método que se ejecuta cada centésima | ||
tic() { | ||
//-- Incrementar en una centesima | ||
this.cent += 1; | ||
|
||
//-- 100 centésimas hacen 1 segundo | ||
if (this.cent == 100) { | ||
this.seg += 1; | ||
this.cent = 0; | ||
} | ||
|
||
//-- 60 segundos hacen un minuto | ||
if (this.seg == 60) { | ||
this.min = 1; | ||
this.seg = 0; | ||
} | ||
|
||
//-- Mostrar el valor actual | ||
this.display.innerHTML = this.min + ":" + this.seg + ":" + this.cent; | ||
} | ||
|
||
//-- Arrancar el cronómetro | ||
start() { | ||
if (!this.timer) { | ||
//-- Lanzar el temporizador para que llame | ||
//-- al método tic cada 10ms (una centésima) | ||
this.timer = setInterval( () => { | ||
this.tic(); | ||
}, 10); | ||
} | ||
} | ||
|
||
//-- Parar el cronómetro | ||
stop() { | ||
if (this.timer) { | ||
clearInterval(this.timer); | ||
this.timer = null; | ||
} | ||
} | ||
|
||
//-- Reset del cronómetro | ||
reset() { | ||
this.cent = 0; | ||
this.seg = 0; | ||
this.min = 0; | ||
|
||
this.display.innerHTML = "0:0:0"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters