From 80ebdcadbc72ae4cca4b8abb8358d8cc0ebe1c83 Mon Sep 17 00:00:00 2001 From: LucasV-IUT Date: Wed, 23 Feb 2022 13:23:22 +0100 Subject: [PATCH] Initial commit --- .gitattributes | 2 ++ .idea/.gitignore | 5 +++ .idea/chronometre.iml | 12 +++++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ Chronometer.js | 81 +++++++++++++++++++++++++++++++++++++++++++ README.md | 2 ++ index.html | 46 ++++++++++++++++++++++++ main.js | 21 +++++++++++ 9 files changed, 183 insertions(+) create mode 100644 .gitattributes create mode 100644 .idea/.gitignore create mode 100644 .idea/chronometre.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 Chronometer.js create mode 100644 README.md create mode 100644 index.html create mode 100644 main.js diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/chronometre.iml b/.idea/chronometre.iml new file mode 100644 index 0000000..8fe3d8c --- /dev/null +++ b/.idea/chronometre.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3668dc8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c03077d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Chronometer.js b/Chronometer.js new file mode 100644 index 0000000..5764677 --- /dev/null +++ b/Chronometer.js @@ -0,0 +1,81 @@ +/* + * Chronometer.js, 23/02/2022 + * no copyright, no rights + */ + +/** + * Simple chronometer + * @author Lucas Vbr + */ +class Chronometer { + + /** + * Create a new Chronometer + * @param element HTML element that will content the display of the chronometer + * @param time Initialize the timer (0 by default) + * @param separator Set the separator between digits (":" by default) + */ + constructor(element, time = 0, separator = ":") { + this.element = element; + this.time = time; + this.isCounting = false; + this.separator = separator; + this.update(); + } + + /** + * Diplay in the HTML element the current value of the timer as HH:mm:ss + */ + update() { + let seconds = this.convertTwoDigits(this.time % 60); + let minuts = this.convertTwoDigits(Math.floor(this.time / 60) % 60); + let hours = this.convertTwoDigits(Math.floor(this.time / 3600) % 24); + + this.element.innerText = hours + this.separator + minuts + this.separator + seconds + } + + /** + * Convert a number with 2 digits string + * @param number Integer number + * @returns {string} Formated String with 2 digits number + */ + convertTwoDigits(number) { + return number.toLocaleString("fr", { + minimumIntegerDigits: 2, + useGrouping: false, + }); + } + + /** + * Start the chronometer + */ + start() { + if (!this.isCounting) { + console.debug("Start"); + this.isCounting = true; + this.interval = setInterval(() => { + this.time++; + this.update(); + }, 1000) + } + } + + /** + * Stop the chronometer + */ + stop() { + if (this.isCounting) { + clearInterval(this.interval); + this.isCounting = false; + } + } + + /** + * Stop the chonometer and set the time to 0 + */ + clear() { + this.stop(); + this.time = 0; + this.update(); /* Update the view */ + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..06032e4 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# chronometer + diff --git a/index.html b/index.html new file mode 100644 index 0000000..b08034a --- /dev/null +++ b/index.html @@ -0,0 +1,46 @@ + + + + + + + + Chronometer + + + +
+
+
+
+
+
+ + + +
+
+
+
+
+ + + + + + + + + \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..f32c382 --- /dev/null +++ b/main.js @@ -0,0 +1,21 @@ +/* Get all elements */ +const element = document.getElementById('time-value'); +const startButton = document.getElementById('start-button'); +const stopButton = document.getElementById('stop-button'); +const clearButton = document.getElementById('clear-button'); + +/* Create a new Chronometer */ +const chrono = new Chronometer(element); + +/* Define actions of buttons */ +startButton.addEventListener("click", () => { + chrono.start() +}); + +stopButton.addEventListener("click", () => { + chrono.stop() +}); + +clearButton.addEventListener("click", () => { + chrono.clear() +}); \ No newline at end of file