質問編集履歴
1
修正依頼に対して、該当コードの追加をしました。
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -4,11 +4,185 @@
|
|
|
4
4
|
|
|
5
5
|
## 該当のソースコード
|
|
6
6
|
下記のボタンの表示、非表示を連続で行いたい
|
|
7
|
+
`<button class="button" id="button">ボタン</button>`
|
|
7
8
|
index.html.erb
|
|
8
9
|
```
|
|
10
|
+
<main class="app">
|
|
11
|
+
<progress id="js-progress" value="0"></progress>
|
|
12
|
+
<div class="progress-bar"></div>
|
|
13
|
+
<div class="timer">
|
|
14
|
+
<div class="button-group mode-buttons" id="js-mode-buttons">
|
|
15
|
+
<button
|
|
16
|
+
data-mode="pomodoro"
|
|
17
|
+
class="button active mode-button"
|
|
18
|
+
id="js-pomodoro"
|
|
19
|
+
>
|
|
20
|
+
Pomodoro
|
|
21
|
+
</button>
|
|
22
|
+
<button
|
|
23
|
+
data-mode="shortBreak"
|
|
24
|
+
class="button mode-button"
|
|
25
|
+
id="js-short-break"
|
|
26
|
+
>
|
|
27
|
+
Short break
|
|
28
|
+
</button>
|
|
29
|
+
<button
|
|
30
|
+
data-mode="longBreak"
|
|
31
|
+
class="button mode-button"
|
|
32
|
+
id="js-long-break"
|
|
33
|
+
>
|
|
34
|
+
Long break
|
|
35
|
+
</button>
|
|
36
|
+
</div>
|
|
37
|
+
<div class="clock" id="js-clock">
|
|
38
|
+
<span id="js-minutes">25</span>
|
|
39
|
+
<span class="separator">:</span>
|
|
40
|
+
<span id="js-seconds">00</span>
|
|
41
|
+
</div>
|
|
9
|
-
<button class="button" id="
|
|
42
|
+
<button class="button" id="button">ボタン</button>
|
|
43
|
+
<button class="main-button" data-action="start" id="js-btn">
|
|
44
|
+
Start
|
|
45
|
+
</button>
|
|
46
|
+
</div>
|
|
47
|
+
</main>
|
|
48
|
+
<%= javascript_pack_tag 'worktimes/time' %>
|
|
49
|
+
|
|
10
50
|
```
|
|
11
51
|
|
|
52
|
+
time.js
|
|
53
|
+
```
|
|
54
|
+
const timer = {
|
|
55
|
+
pomodoro: 25,
|
|
56
|
+
shortBreak: 5,
|
|
57
|
+
longBreak: 15,
|
|
58
|
+
longBreakInterval: 4,
|
|
59
|
+
sessions: 0,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let interval;
|
|
63
|
+
|
|
64
|
+
const mainButton = document.getElementById('js-btn');
|
|
65
|
+
mainButton.addEventListener('click', () => {
|
|
66
|
+
|
|
67
|
+
const { action } = mainButton.dataset;
|
|
68
|
+
if (action === 'start') {
|
|
69
|
+
startTimer();
|
|
70
|
+
} else {
|
|
71
|
+
stopTimer();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const modeButtons = document.querySelector('#js-mode-buttons');
|
|
76
|
+
modeButtons.addEventListener('click', handleMode);
|
|
77
|
+
|
|
78
|
+
function getRemainingTime(endTime) {
|
|
79
|
+
const currentTime = Date.parse(new Date());
|
|
80
|
+
const difference = endTime - currentTime;
|
|
81
|
+
|
|
82
|
+
const total = Number.parseInt(difference / 1000, 10);
|
|
83
|
+
const minutes = Number.parseInt((total / 60) % 60, 10);
|
|
84
|
+
const seconds = Number.parseInt(total % 60, 10);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
total,
|
|
88
|
+
minutes,
|
|
89
|
+
seconds,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function startTimer() {
|
|
94
|
+
let { total } = timer.remainingTime;
|
|
95
|
+
const endTime = Date.parse(new Date()) + total * 1000;
|
|
96
|
+
|
|
97
|
+
if (timer.mode === 'pomodoro') timer.sessions++;
|
|
98
|
+
|
|
99
|
+
mainButton.dataset.action = 'stop';
|
|
100
|
+
mainButton.textContent = 'stop';
|
|
101
|
+
mainButton.classList.add('active');
|
|
102
|
+
|
|
103
|
+
interval = setInterval(function() {
|
|
104
|
+
timer.remainingTime = getRemainingTime(endTime);
|
|
105
|
+
updateClock();
|
|
106
|
+
|
|
107
|
+
total = timer.remainingTime.total;
|
|
108
|
+
if (total <= 0) {
|
|
109
|
+
clearInterval(interval);
|
|
110
|
+
|
|
111
|
+
switch (timer.mode) {
|
|
112
|
+
case 'pomodoro':
|
|
113
|
+
if (timer.sessions % timer.longBreakInterval === 0) {
|
|
114
|
+
switchMode('longBreak');
|
|
115
|
+
} else {
|
|
116
|
+
switchMode('shortBreak');
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
switchMode('pomodoro');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}, 1000);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function stopTimer() {
|
|
127
|
+
clearInterval(interval);
|
|
128
|
+
|
|
129
|
+
mainButton.dataset.action = 'start';
|
|
130
|
+
mainButton.textContent = 'start';
|
|
131
|
+
mainButton.classList.remove('active');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function updateClock() {
|
|
135
|
+
const { remainingTime } = timer;
|
|
136
|
+
const minutes = `${remainingTime.minutes}`.padStart(2, '0');
|
|
137
|
+
const seconds = `${remainingTime.seconds}`.padStart(2, '0');
|
|
138
|
+
|
|
139
|
+
const min = document.getElementById('js-minutes');
|
|
140
|
+
const sec = document.getElementById('js-seconds');
|
|
141
|
+
min.textContent = minutes;
|
|
142
|
+
sec.textContent = seconds;
|
|
143
|
+
|
|
144
|
+
const text = timer.mode === 'pomodoro' ? 'Pomosto' : 'Take a break!';
|
|
145
|
+
document.title = `${minutes}:${seconds} — ${text}`;
|
|
146
|
+
|
|
147
|
+
const progress = document.getElementById('js-progress');
|
|
148
|
+
progress.value = timer[timer.mode] * 60 - timer.remainingTime.total;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
function switchMode(mode) {
|
|
153
|
+
timer.mode = mode;
|
|
154
|
+
timer.remainingTime = {
|
|
155
|
+
total: timer[mode] * 60,
|
|
156
|
+
minutes: timer[mode],
|
|
157
|
+
seconds: 0,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
document
|
|
161
|
+
.querySelectorAll('button[data-mode]')
|
|
162
|
+
.forEach(e => e.classList.remove('active'));
|
|
163
|
+
document.querySelector(`[data-mode="${mode}"]`).classList.add('active');
|
|
164
|
+
document.body.style.backgroundColor = `var(--${mode})`;
|
|
165
|
+
document
|
|
166
|
+
.getElementById('js-progress')
|
|
167
|
+
.setAttribute('max', timer.remainingTime.total);
|
|
168
|
+
updateClock();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function handleMode(event) {
|
|
172
|
+
const { mode } = event.target.dataset;
|
|
173
|
+
|
|
174
|
+
if (!mode) return;
|
|
175
|
+
|
|
176
|
+
switchMode(mode);
|
|
177
|
+
stopTimer();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
181
|
+
switchMode('pomodoro');
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
|
|
12
186
|
## 試したこと
|
|
13
187
|
`document.getElementById('btn').style.display = 'none';`
|
|
14
188
|
で非表示にしておき、休憩時間のタイマーの処理の中に
|