Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (84 loc) · 2.77 KB
/
Copy pathscript.js
File metadata and controls
93 lines (84 loc) · 2.77 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
93
const timeText = document.getElementById('timeText');
const modeLabel = document.getElementById('modeLabel');
const startBtn = document.getElementById('startBtn');
const resetBtn = document.getElementById('resetBtn');
const ringFill = document.getElementById('ringFill');
const modeBtns = document.querySelectorAll('.mode-btn');
const pomodoroCountEl = document.getElementById('pomodoroCount');
const totalTimeEl = document.getElementById('totalTime');
const RADIUS = 120;
const CIRCUMFERENCE = 2 * Math.PI * RADIUS;
ringFill.style.strokeDasharray = CIRCUMFERENCE;
ringFill.style.strokeDashoffset = 0;
let totalSeconds = 25 * 60;
let remainingSeconds = totalSeconds;
let interval = null;
let isRunning = false;
let currentMode = 'pomodoro';
let pomodoroCount = 0;
let totalFocusMin = 0;
function formatTime(sec) {
const m = Math.floor(sec / 60);
const s = sec % 60;
return `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
}
function updateDisplay() {
timeText.textContent = formatTime(remainingSeconds);
const progress = 1 - remainingSeconds / totalSeconds;
ringFill.style.strokeDashoffset = CIRCUMFERENCE * progress;
}
function setMode(mode, minutes) {
clearInterval(interval);
isRunning = false;
currentMode = mode;
totalSeconds = minutes * 60;
remainingSeconds = totalSeconds;
startBtn.innerHTML = '<i class="fa-solid fa-play"></i> Start';
modeLabel.textContent = mode === 'pomodoro' ? 'Time to focus!' : mode === 'short' ? 'Short break' : 'Long break';
updateDisplay();
}
function start() {
if (isRunning) {
clearInterval(interval);
isRunning = false;
startBtn.innerHTML = '<i class="fa-solid fa-play"></i> Start';
return;
}
isRunning = true;
startBtn.innerHTML = '<i class="fa-solid fa-pause"></i> Pause';
interval = setInterval(() => {
remainingSeconds--;
updateDisplay();
if (remainingSeconds <= 0) {
clearInterval(interval);
isRunning = false;
startBtn.innerHTML = '<i class="fa-solid fa-play"></i> Start';
if (currentMode === 'pomodoro') {
pomodoroCount++;
totalFocusMin += 25;
pomodoroCountEl.textContent = pomodoroCount;
totalTimeEl.textContent = `${totalFocusMin}m`;
modeBtns[1].click();
} else {
modeBtns[0].click();
}
}
}, 1000);
}
function reset() {
clearInterval(interval);
isRunning = false;
remainingSeconds = totalSeconds;
startBtn.innerHTML = '<i class="fa-solid fa-play"></i> Start';
updateDisplay();
}
modeBtns.forEach(btn => {
btn.addEventListener('click', () => {
modeBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
setMode(btn.dataset.mode, parseInt(btn.dataset.time));
});
});
startBtn.addEventListener('click', start);
resetBtn.addEventListener('click', reset);
updateDisplay();