forked from AnujShrivastava01/AnimateItNow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpomodoro.js
More file actions
92 lines (82 loc) · 3.08 KB
/
pomodoro.js
File metadata and controls
92 lines (82 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
document.addEventListener('DOMContentLoaded', () => {
const timeDisplay = document.querySelector('.time-display');
const startBtn = document.getElementById('start-btn');
const pauseBtn = document.getElementById('pause-btn');
const resetBtn = document.getElementById('reset-btn');
const pomodoroBtn = document.getElementById('pomodoro-btn');
const shortBreakBtn = document.getElementById('short-break-btn');
const longBreakBtn = document.getElementById('long-break-btn');
let timerInterval;
let isRunning = false;
let timeRemaining = 25 * 60; // Default Pomodoro time in seconds
let currentMode = 'pomodoro';
const pomodoroTime = 25 * 60;
const shortBreakTime = 5 * 60;
const longBreakTime = 15 * 60;
function updateDisplay() {
const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;
timeDisplay.textContent = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
function startTimer() {
if (isRunning) return;
isRunning = true;
timerInterval = setInterval(() => {
if (timeRemaining > 0) {
timeRemaining--;
updateDisplay();
} else {
clearInterval(timerInterval);
isRunning = false;
alert('Time\'s up!');
}
}, 1000);
}
function pauseTimer() {
clearInterval(timerInterval);
isRunning = false;
}
function resetTimer() {
pauseTimer();
switch (currentMode) {
case 'pomodoro':
timeRemaining = pomodoroTime;
break;
case 'short-break':
timeRemaining = shortBreakTime;
break;
case 'long-break':
timeRemaining = longBreakTime;
break;
}
updateDisplay();
// Update active button
document.querySelector('.modes .active').classList.remove('active');
document.getElementById(`${currentMode}-btn`).classList.add('active');
}
function setMode(mode) {
pauseTimer();
currentMode = mode;
switch (mode) {
case 'pomodoro':
timeRemaining = pomodoroTime;
break;
case 'short-break':
timeRemaining = shortBreakTime;
break;
case 'long-break':
timeRemaining = longBreakTime;
break;
}
updateDisplay();
// Update active button
document.querySelector('.modes .active').classList.remove('active');
document.getElementById(`${mode}-btn`).classList.add('active');
}
startBtn.addEventListener('click', startTimer);
pauseBtn.addEventListener('click', pauseTimer);
resetBtn.addEventListener('click', resetTimer);
pomodoroBtn.addEventListener('click', () => setMode('pomodoro'));
shortBreakBtn.addEventListener('click', () => setMode('short-break'));
longBreakBtn.addEventListener('click', () => setMode('long-break'));
});