回答編集履歴

1

タイマーをオブジェクトとして正しく扱うように修正

2016/12/24 22:59

投稿

naomi3
naomi3

スコア1105

test CHANGED
@@ -1,5 +1,15 @@
1
1
  これでどうでしょうか。
2
2
 
3
+
4
+
5
+ 修正
6
+
7
+
8
+
9
+ タイマーをオブジェクトとして正しく扱うようにしました。
10
+
11
+ デザインについては自信がありません:-)
12
+
3
13
  ```HTML
4
14
 
5
15
  <!DOCTYPE HTML>
@@ -114,51 +124,197 @@
114
124
 
115
125
  <script>
116
126
 
117
- var starter = document.getElementById('starter');
127
+ var starter = document.querySelector('#starter');
118
-
128
+
119
- var stopper = document.getElementById('stopper');
129
+ var stopper = document.querySelector('#stopper');
120
-
130
+
131
+
132
+
133
+
134
+
121
- //タイマー用オブジェクトを格納する配列
135
+ //タイマーインスタンスを格納する配列
122
136
 
123
137
  var timerArray = [
124
138
 
139
+ new Timer('label0', 'minBox0', 'secBox0'),
140
+
141
+ new Timer('label1', 'minBox1', 'secBox1'),
142
+
143
+ new Timer('label2', 'minBox2', 'secBox2'),
144
+
145
+ ];
146
+
147
+
148
+
149
+
150
+
151
+ //タイマーのコンストラクタ
152
+
153
+ function Timer(label, minBox, secBox) {
154
+
155
+ this.intervalID = null; // nullのとき停止中
156
+
157
+ this.label = document.querySelector('#' + label);
158
+
159
+ this.minBox = document.querySelector('#' + minBox);
160
+
161
+ this.secBox = document.querySelector('#' + secBox);
162
+
163
+ }
164
+
165
+
166
+
167
+
168
+
169
+ //タイマー開始メソッド
170
+
171
+ Timer.prototype.startTimer = function() {
172
+
173
+ this.stopTimer()
174
+
175
+
176
+
177
+ var min = this.minBox.value;
178
+
179
+ var sec = this.secBox.value;
180
+
181
+
182
+
183
+ if (min === "" && sec === "") {
184
+
185
+ alert(this.label.innerText + "に時間を設定してください!");
186
+
187
+ return;
188
+
189
+ }
190
+
191
+
192
+
193
+ min = (min === "" ? 0 : parseInt(min, 10));
194
+
195
+ sec = (sec === "" ? 0 : parseInt(sec, 10));
196
+
197
+
198
+
199
+ //不正な数値の場合
200
+
201
+ if (! isFinite(min) || ! isFinite(sec) ||
202
+
203
+ min < 0 || sec < 0 || (min === 0 && sec === 0))
204
+
125
205
  {
126
206
 
127
- intervalID : null, //インターバルID保持する(nullのとき停止中)
128
-
129
- label : document.getElementById('label0'),
130
-
131
- minBox : document.getElementById('minBox0'),
132
-
133
- secBox : document.getElementById('secBox0')
134
-
135
- },
136
-
137
- {
138
-
139
- intervalID : null,
140
-
141
- label : document.getElementById('label1'),
142
-
143
- minBox : document.getElementById('minBox1'),
144
-
145
- secBox : document.getElementById('secBox1')
146
-
147
- },
148
-
149
- {
150
-
151
- intervalID : null,
152
-
153
- label : document.getElementById('label2'),
154
-
155
- minBox : document.getElementById('minBox2'),
156
-
157
- secBox : document.getElementById('secBox2')
158
-
159
- }
160
-
161
- ];
207
+ alert(this.label.innerText + "に正しい時間設定してください!");
208
+
209
+ return;
210
+
211
+ }
212
+
213
+
214
+
215
+ this.minBox.readonly = true;
216
+
217
+ this.secBox.readonly = true;
218
+
219
+
220
+
221
+ this.minBox.value = min;
222
+
223
+ this.secBox.value = sec;
224
+
225
+
226
+
227
+ this.countDown();
228
+
229
+ };
230
+
231
+
232
+
233
+
234
+
235
+ //カウントダウンする関数を1000ミリ秒毎に呼び出すメソッド
236
+
237
+ Timer.prototype.countDown = function()
238
+
239
+ {
240
+
241
+ //setIntervalのコールバック関数ではthisが変わるのでtimerで代用
242
+
243
+ var timer = this;
244
+
245
+
246
+
247
+ this.intervalID = setInterval(function() {
248
+
249
+ var min = parseInt(timer.minBox.value, 10);
250
+
251
+ var sec = parseInt(timer.secBox.value, 10);
252
+
253
+ var totalSec = min * 60 + sec - 1;
254
+
255
+
256
+
257
+ if (totalSec <= 0) {
258
+
259
+ timer.stopTimer();
260
+
261
+ totalSec = 0;
262
+
263
+ }
264
+
265
+
266
+
267
+ //残り分数はintを60で割って切り捨てる
268
+
269
+ timer.minBox.value = Math.floor(totalSec / 60);
270
+
271
+ //残り秒数はintを60で割った余り
272
+
273
+ timer.secBox.value = totalSec % 60;
274
+
275
+ }, 1000);
276
+
277
+ };
278
+
279
+
280
+
281
+
282
+
283
+ //タイマー停止メソッド
284
+
285
+ Timer.prototype.stopTimer = function() {
286
+
287
+ if (this.intervalID !== null) {
288
+
289
+ clearInterval(this.intervalID);
290
+
291
+ this.intervalID = null;
292
+
293
+ }
294
+
295
+
296
+
297
+ this.minBox.readonly = false;
298
+
299
+ this.secBox.readonly = false;
300
+
301
+ };
302
+
303
+
304
+
305
+
306
+
307
+ //全タイマー開始関数
308
+
309
+ function startTimers() {
310
+
311
+ for (var no in timerArray) {
312
+
313
+ timerArray[no].startTimer();
314
+
315
+ }
316
+
317
+ }
162
318
 
163
319
 
164
320
 
@@ -166,151 +322,11 @@
166
322
 
167
323
  //全タイマー停止関数
168
324
 
169
- function stopTimers()
325
+ function stopTimers() {
170
-
171
- {
172
326
 
173
327
  for (var no in timerArray) {
174
328
 
175
- clearTimer(timerArray[no]);
176
-
177
- }
178
-
179
- }
180
-
181
-
182
-
183
-
184
-
185
- //全タイマー開始関数
186
-
187
- function startTimers()
188
-
189
- {
190
-
191
- for (var no in timerArray) {
192
-
193
- var timer = timerArray[no];
194
-
195
- clearTimer(timer)
196
-
197
-
198
-
199
- var min = timer.minBox.value;
200
-
201
- var sec = timer.secBox.value;
202
-
203
-
204
-
205
- if (min === "" && sec === "") {
206
-
207
- alert(timer.label.innerText + "に時間を設定してください!");
208
-
209
- continue;
210
-
211
- }
212
-
213
-
214
-
215
- min = (min === "" ? 0 : parseInt(min, 10));
216
-
217
- sec = (sec === "" ? 0 : parseInt(sec, 10));
218
-
219
-
220
-
221
- //不正な数値の場合
222
-
223
- if (! isFinite(min) || ! isFinite(sec) ||
224
-
225
- min < 0 || sec < 0 || (min === 0 && sec === 0)) {
226
-
227
- alert(timer.label.innerText + "に正しい時間を設定してください!");
228
-
229
- continue;
230
-
231
- }
232
-
233
-
234
-
235
- timer.minBox.value = min;
329
+ timerArray[no].stopTimer();
236
-
237
- timer.secBox.value = sec;
238
-
239
-
240
-
241
- timer.minBox.disabled = true;
242
-
243
- timer.secBox.disabled = true;
244
-
245
-
246
-
247
- countDown(timer);
248
-
249
- }
250
-
251
- }
252
-
253
-
254
-
255
-
256
-
257
- //カウントダウンする関数を1000ミリ秒毎に呼び出す関数
258
-
259
- function countDown(timer)
260
-
261
- {
262
-
263
- timer.intervalID = setInterval(function() {
264
-
265
- var min = parseInt(timer.minBox.value, 10);
266
-
267
- var sec = parseInt(timer.secBox.value, 10);
268
-
269
- var totalSec = min * 60 + sec - 1;
270
-
271
-
272
-
273
- if (totalSec < 0) {
274
-
275
- clearTimer(timer);
276
-
277
- }
278
-
279
- else {
280
-
281
- //残り分数はintを60で割って切り捨てる
282
-
283
- timer.minBox.value = Math.floor(totalSec / 60);
284
-
285
- //残り秒数はintを60で割った余り
286
-
287
- timer.secBox.value = totalSec % 60;
288
-
289
- }
290
-
291
- }, 1000);
292
-
293
- }
294
-
295
-
296
-
297
-
298
-
299
- //タイマー停止関数
300
-
301
- function clearTimer(timer)
302
-
303
- {
304
-
305
- if (timer.intervalID !== null) {
306
-
307
- clearInterval(timer.intervalID);
308
-
309
- timer.intervalID = null;
310
-
311
- timer.minBox.disabled = false;
312
-
313
- timer.secBox.disabled = false;
314
330
 
315
331
  }
316
332