From 0f9e08cb6bd53ff3cc4f81a588d58e69425efaa9 Mon Sep 17 00:00:00 2001 From: OlgaML Date: Thu, 24 Jul 2025 12:09:34 +0200 Subject: [PATCH] JS & DOM practice --- src/chronometer.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/chronometer.js b/src/chronometer.js index 83c75bd2..4527e4ed 100644 --- a/src/chronometer.js +++ b/src/chronometer.js @@ -1,33 +1,60 @@ class Chronometer { constructor() { - // ... your code goes here + this.currentTime = 0; + this.intervalId = null; } start(printTimeCallback) { - // ... your code goes here + if (this.intervalId) return; // Prevent multiple intervals + this.intervalId = setInterval(() => { + this.currentTime++; + if (printTimeCallback) { + printTimeCallback(); + } + }, 1000); } getMinutes() { // ... your code goes here + return Math.floor(this.currentTime / 60); } getSeconds() { // ... your code goes here + return this.currentTime % 60; } + getMilliseconds() { + // ... your code goes here + return (this.currentTime % 1) * 1000; + } + computeTwoDigitNumber(value) { // ... your code goes here + return value < 10 ? `0${value}` : `${value}`; } stop() { // ... your code goes here + if (this.intervalId) { + clearInterval(this.intervalId); + this.intervalId = null; + } } reset() { // ... your code goes here + this.currentTime = 0; + if (this.intervalId) { + clearInterval(this.intervalId); + this.intervalId = null; + } } split() { // ... your code goes here + const minutes = this.computeTwoDigitNumber(this.getMinutes()); + const seconds = this.computeTwoDigitNumber(this.getSeconds()); + return `${minutes}:${seconds}`; } }