From c92c8d074a93e9ffd51e646f13ef65e01a2a4e1d Mon Sep 17 00:00:00 2001 From: Max Stevenson Date: Tue, 9 Jun 2020 17:54:13 +0100 Subject: [PATCH] Added very basic 2 minute countdown timer logic. --- index.html | 7 ++++++- public/scripts/app.js | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 public/scripts/app.js diff --git a/index.html b/index.html index 208c949..4765fc0 100644 --- a/index.html +++ b/index.html @@ -346,7 +346,11 @@
-

Timer

+

Countdown Clock

+
+ +
+
@@ -354,4 +358,5 @@ + diff --git a/public/scripts/app.js b/public/scripts/app.js new file mode 100644 index 0000000..316f742 --- /dev/null +++ b/public/scripts/app.js @@ -0,0 +1,24 @@ +function startTimer(duration, display) { + var timer = duration, + minutes, + seconds; + setInterval(function() { + minutes = parseInt(timer / 60, 10); + seconds = parseInt(timer % 60, 10); + + minutes = minutes < 10 ? "0" + minutes : minutes; + seconds = seconds < 10 ? "0" + seconds : seconds; + + display.textContent = minutes + ":" + seconds; + + if (--timer < 0) { + timer = duration; + } + }, 1000); +} + +document.getElementById("start-timer").addEventListener("click", () => { + var twoMinutes = 60 * 2, + display = document.getElementById("countdown-timer__display"); + startTimer(twoMinutes, display); +});