Changed around line 1
+ class Chronometer {
+ constructor() {
+ this.isRunning = false;
+ this.startTime = 0;
+ this.elapsedTime = 0;
+ this.intervalId = null;
+ this.laps = [];
+
+ this.display = {
+ hours: document.querySelector('.hours'),
+ minutes: document.querySelector('.minutes'),
+ seconds: document.querySelector('.seconds'),
+ milliseconds: document.querySelector('.milliseconds')
+ };
+
+ this.buttons = {
+ start: document.querySelector('.start-btn'),
+ lap: document.querySelector('.lap-btn'),
+ reset: document.querySelector('.reset-btn')
+ };
+
+ this.lapList = document.querySelector('.lap-times');
+
+ this.bindEvents();
+ }
+
+ bindEvents() {
+ this.buttons.start.addEventListener('click', () => this.toggleStart());
+ this.buttons.lap.addEventListener('click', () => this.addLap());
+ this.buttons.reset.addEventListener('click', () => this.reset());
+ }
+
+ toggleStart() {
+ if (!this.isRunning) {
+ this.startTime = Date.now() - this.elapsedTime;
+ this.intervalId = setInterval(() => this.updateDisplay(), 10);
+ this.buttons.start.textContent = 'Stop';
+ this.buttons.start.style.background = 'var(--secondary)';
+ } else {
+ clearInterval(this.intervalId);
+ this.buttons.start.textContent = 'Start';
+ this.buttons.start.style.background = 'var(--accent)';
+ }
+ this.isRunning = !this.isRunning;
+ }
+
+ updateDisplay() {
+ this.elapsedTime = Date.now() - this.startTime;
+ const time = this.formatTime(this.elapsedTime);
+
+ this.display.hours.textContent = time.hours.padStart(2, '0');
+ this.display.minutes.textContent = time.minutes.padStart(2, '0');
+ this.display.seconds.textContent = time.seconds.padStart(2, '0');
+ this.display.milliseconds.textContent = time.milliseconds.padStart(2, '0');
+ }
+
+ formatTime(time) {
+ const ms = Math.floor((time % 1000) / 10);
+ const s = Math.floor((time / 1000) % 60);
+ const m = Math.floor((time / (1000 * 60)) % 60);
+ const h = Math.floor(time / (1000 * 60 * 60));
+
+ return {
+ hours: h.toString(),
+ minutes: m.toString(),
+ seconds: s.toString(),
+ milliseconds: ms.toString()
+ };
+ }
+
+ addLap() {
+ if (!this.isRunning) return;
+
+ const lapTime = this.formatTime(this.elapsedTime);
+ const lapElement = document.createElement('li');
+ lapElement.textContent = `${lapTime.hours}:${lapTime.minutes}:${lapTime.seconds}.${lapTime.milliseconds}`;
+ this.lapList.insertBefore(lapElement, this.lapList.firstChild);
+ }
+
+ reset() {
+ clearInterval(this.intervalId);
+ this.isRunning = false;
+ this.elapsedTime = 0;
+ this.buttons.start.textContent = 'Start';
+ this.buttons.start.style.background = 'var(--accent)';
+ this.updateDisplay();
+ this.lapList.innerHTML = '';
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new Chronometer();
+ });