From 581ac96acab74f3e48e81bed492d47aebbe8c691 Mon Sep 17 00:00:00 2001 From: Tchi <86352901+LucasVbr@users.noreply.github.com> Date: Sun, 4 Jul 2021 12:33:35 +0200 Subject: [PATCH] init Timer --- README.md | 4 ++-- css/style.css | 8 ++++++++ index.htm | 13 +++++++++++++ js/main.js | 28 ++++++++++++++++++++++++++++ js/timer.js | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 css/style.css create mode 100644 index.htm create mode 100644 js/main.js create mode 100644 js/timer.js diff --git a/README.md b/README.md index 18f7468..befb23f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# javascript-timer -Make a time in JavaScript +# JavaScript Timer +Objective : Make a timer in JavaScript \ No newline at end of file diff --git a/css/style.css b/css/style.css new file mode 100644 index 0000000..72af810 --- /dev/null +++ b/css/style.css @@ -0,0 +1,8 @@ +body { + background-color: whitesmoke; +} + +p { + font-size: 200px; + text-align: center; +} \ No newline at end of file diff --git a/index.htm b/index.htm new file mode 100644 index 0000000..54ba83a --- /dev/null +++ b/index.htm @@ -0,0 +1,13 @@ + + + + + + + Timer + + +

+ + + \ No newline at end of file diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..b661f40 --- /dev/null +++ b/js/main.js @@ -0,0 +1,28 @@ +var htmlTimer = document.getElementById("timer"); + +var timer = setInterval(timerLoop, 1000); +const queryString = window.location.search; +const urlParams = new URLSearchParams(queryString); +let time = parseInt(urlParams.get('time')) + + +function timerLoop() { + htmlTimer.innerHTML = convertSecMin(time); + time--; + if(time < 0) { + clearInterval(timer) + htmlTimer.style.color = "red"; + } +} + +function convertSecMin(value) { + let seconds = Math.floor(value % 60); + let minuts = Math.floor(value / 60); + return `${convertDigits(minuts)}:${convertDigits(seconds)}` +} + +function convertDigits(number) { + return number.toLocaleString('en-US', { + minimumIntegerDigits : 2, + useGrouping : false}); +} \ No newline at end of file diff --git a/js/timer.js b/js/timer.js new file mode 100644 index 0000000..387d10b --- /dev/null +++ b/js/timer.js @@ -0,0 +1,37 @@ +class Timer { + + /** + * Make a timer + * @param {int} time in seconds + * @throws TypeError if time is not a positive Integer + */ + constructor(time) { + if (!isValid(time)) throw new TypeError(`${time} n'est pas un entier valide`); + this.time = time; + } + + + /** + * Predicate that show if the value of the timer is correctly set + * @param {int} valueTest + * @return True if the predicate is verified + * False else + */ + isValid(valueTest) { + return Number.isInteger(valueTest) && valueTest > 0 + } + + /** + * @return the current time + */ + getTime() { + return this.time + } + + /** + * Make start the timer + */ + startTimer() { + setTimeout(this.time--, 1000) + } +} \ No newline at end of file