mirror of
https://github.com/LucasVbr/timer.git
synced 2026-05-13 17:22:04 +00:00
init Timer
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
# javascript-timer
|
# JavaScript Timer
|
||||||
Make a time in JavaScript
|
Objective : Make a timer in JavaScript
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
body {
|
||||||
|
background-color: whitesmoke;
|
||||||
|
}
|
||||||
|
|
||||||
|
p {
|
||||||
|
font-size: 200px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link rel="stylesheet" href="./css/style.css">
|
||||||
|
<title>Timer</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p id="timer"></p>
|
||||||
|
<script src="./js/main.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
+28
@@ -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});
|
||||||
|
}
|
||||||
+37
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user