Changed around line 1
+ class PomodoroTimer {
+ constructor() {
+ this.minutes = 25;
+ this.seconds = 0;
+ this.isRunning = false;
+ this.isWorkTime = true;
+ this.timer = null;
+
+ this.initializeElements();
+ this.initializeEventListeners();
+ }
+
+ initializeElements() {
+ this.minutesDisplay = document.getElementById('minutes');
+ this.secondsDisplay = document.getElementById('seconds');
+ this.startBtn = document.getElementById('startBtn');
+ this.resetBtn = document.getElementById('resetBtn');
+ this.timerState = document.getElementById('timerState');
+ this.workTimeInput = document.getElementById('workTime');
+ this.breakTimeInput = document.getElementById('breakTime');
+ this.alertSound = document.getElementById('alertSound');
+ }
+
+ initializeEventListeners() {
+ this.startBtn.addEventListener('click', () => this.toggleTimer());
+ this.resetBtn.addEventListener('click', () => this.resetTimer());
+ this.workTimeInput.addEventListener('change', () => this.updateWorkTime());
+ this.breakTimeInput.addEventListener('change', () => this.updateBreakTime());
+ }
+
+ toggleTimer() {
+ if (this.isRunning) {
+ this.pauseTimer();
+ this.startBtn.textContent = 'Start';
+ } else {
+ this.startTimer();
+ this.startBtn.textContent = 'Pause';
+ }
+ this.isRunning = !this.isRunning;
+ }
+
+ startTimer() {
+ this.timer = setInterval(() => {
+ if (this.seconds === 0) {
+ if (this.minutes === 0) {
+ this.switchMode();
+ return;
+ }
+ this.minutes--;
+ this.seconds = 59;
+ } else {
+ this.seconds--;
+ }
+ this.updateDisplay();
+ }, 1000);
+ }
+
+ pauseTimer() {
+ clearInterval(this.timer);
+ }
+
+ resetTimer() {
+ this.pauseTimer();
+ this.isRunning = false;
+ this.startBtn.textContent = 'Start';
+ this.isWorkTime = true;
+ this.minutes = parseInt(this.workTimeInput.value);
+ this.seconds = 0;
+ this.timerState.textContent = 'Work Time';
+ this.updateDisplay();
+ document.body.classList.remove('flashing');
+ }
+
+ switchMode() {
+ this.isWorkTime = !this.isWorkTime;
+ this.minutes = this.isWorkTime ?
+ parseInt(this.workTimeInput.value) :
+ parseInt(this.breakTimeInput.value);
+ this.seconds = 0;
+ this.timerState.textContent = this.isWorkTime ? 'Work Time' : 'Break Time';
+ this.alertUser();
+ this.updateDisplay();
+ }
+
+ alertUser() {
+ document.body.classList.add('flashing');
+ this.alertSound.play();
+ setTimeout(() => {
+ document.body.classList.remove('flashing');
+ }, 3000);
+ }
+
+ updateDisplay() {
+ this.minutesDisplay.textContent = String(this.minutes).padStart(2, '0');
+ this.secondsDisplay.textContent = String(this.seconds).padStart(2, '0');
+ }
+
+ updateWorkTime() {
+ if (this.isWorkTime && !this.isRunning) {
+ this.minutes = parseInt(this.workTimeInput.value);
+ this.seconds = 0;
+ this.updateDisplay();
+ }
+ }
+
+ updateBreakTime() {
+ if (!this.isWorkTime && !this.isRunning) {
+ this.minutes = parseInt(this.breakTimeInput.value);
+ this.seconds = 0;
+ this.updateDisplay();
+ }
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new PomodoroTimer();
+ });