From 94bf1841b0bf0c7df517763aed3edea6c3160ace Mon Sep 17 00:00:00 2001 From: Jiban Kumar Majumdar Date: Wed, 7 Aug 2024 08:47:17 +0530 Subject: [PATCH] Add files via upload --- timer.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 timer.js diff --git a/timer.js b/timer.js new file mode 100644 index 0000000..947c4a1 --- /dev/null +++ b/timer.js @@ -0,0 +1,34 @@ +let timers = {}; +let intervals = {}; + +function startTimer(timerId) { + if (intervals[timerId]) { + return; // Timer is already running + } + + if (!timers[timerId]) { + timers[timerId] = 0; + } + + intervals[timerId] = setInterval(() => { + timers[timerId]++; + document.getElementById(timerId).textContent = formatTime(timers[timerId]); + }, 1000); +} + +function stopTimer(timerId) { + clearInterval(intervals[timerId]); + intervals[timerId] = null; +} + +function resetTimer(timerId) { + stopTimer(timerId); + timers[timerId] = 0; + document.getElementById(timerId).textContent = "00:00"; +} + +function formatTime(seconds) { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`; +}