回答編集履歴

3

調整版

2020/09/26 10:32

投稿

yambejp
yambejp

スコア116734

test CHANGED
@@ -211,3 +211,137 @@
211
211
  </div>
212
212
 
213
213
  ```
214
+
215
+
216
+
217
+ # 調整版
218
+
219
+ ```javascript
220
+
221
+ <script>
222
+
223
+ 'use strict';
224
+
225
+ var current_d=0;
226
+
227
+ var prev_d=0;
228
+
229
+ window.addEventListener('DOMContentLoaded', ()=>{
230
+
231
+ const timer = document.querySelector('#timer');
232
+
233
+ const start = document.querySelector('#start');
234
+
235
+ const stop = document.querySelector('#stop');
236
+
237
+ const reset = document.querySelector('#reset');
238
+
239
+ const mintus = document.querySelector('#mintus');
240
+
241
+ const math = document.querySelector('#sum');
242
+
243
+ const sum_account = document.querySelector('#sum_account');
244
+
245
+ let timerId;
246
+
247
+
248
+
249
+ stop.disabled=true;
250
+
251
+
252
+
253
+ const format_time=(d)=>{
254
+
255
+ const m = parseInt(d/60/1000).toString().padStart(2, '0');
256
+
257
+ const s = (parseInt(d/1000)%60).toString().padStart(2, '0');
258
+
259
+ const ms = (d%1000).toString().padStart(3, '0');
260
+
261
+ return `${m}:${s}.${ms}`;
262
+
263
+ };
264
+
265
+ start.addEventListener('click',()=>{
266
+
267
+ var now=new Date();
268
+
269
+ start.disabled=true;
270
+
271
+ stop.disabled=false;
272
+
273
+ reset.disabled=true;
274
+
275
+ timerId=setInterval(()=>{
276
+
277
+ current_d=new Date().getTime()-now.getTime()+prev_d;
278
+
279
+ timer.textContent=format_time(current_d);
280
+
281
+ },10);
282
+
283
+ });
284
+
285
+ stop.addEventListener('click',()=>{
286
+
287
+ start.disabled=false;
288
+
289
+ stop.disabled=true;
290
+
291
+ reset.disabled=false;
292
+
293
+ clearInterval(timerId);
294
+
295
+ prev_d=current_d;
296
+
297
+ });
298
+
299
+ reset.addEventListener('click',()=>{
300
+
301
+ current_d=0;
302
+
303
+ prev_d=0;
304
+
305
+ timer.textContent=format_time(current_d);
306
+
307
+ });
308
+
309
+ math.addEventListener('click',()=>{
310
+
311
+ const m=parseInt(mintus.options[mintus.selectedIndex].textContent);
312
+
313
+ sum_account.textContent=format_time(m*60*1000-current_d);
314
+
315
+ });
316
+
317
+ });
318
+
319
+ </script>
320
+
321
+ <div id="timer">00:00.000</div>
322
+
323
+ <button id="start">Start</button>
324
+
325
+ <button id="stop">Stop</button>
326
+
327
+ <button id="reset">Reset</button>
328
+
329
+ <select name="mintus" id ="mintus">
330
+
331
+ <option value="time_1">30</option>
332
+
333
+ <option value="time_2">60</option>
334
+
335
+ <option value="time_3">90</option>
336
+
337
+ </select>
338
+
339
+ <div>
340
+
341
+ <button id="sum">計算</button>
342
+
343
+ <div id="sum_account" ></div>
344
+
345
+ </div>
346
+
347
+ ```

2

chousei

2020/09/26 10:32

投稿

yambejp
yambejp

スコア116734

test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  'use strict';
10
10
 
11
- var d;
11
+ var d=new Date(0);
12
12
 
13
13
  window.addEventListener('DOMContentLoaded', ()=>{
14
14
 
@@ -38,7 +38,7 @@
38
38
 
39
39
  function format_time(d){
40
40
 
41
- const m = String(d.getMinutes()).padStart(2, '0');
41
+ const m = String(d.getMinutes()+(d.getHours()-9)*60).padStart(2, '0');
42
42
 
43
43
  const s = String(d.getSeconds()).padStart(2, '0');
44
44
 

1

chosei

2020/09/26 09:44

投稿

yambejp
yambejp

スコア116734

test CHANGED
@@ -108,9 +108,7 @@
108
108
 
109
109
  const m = parseInt(mintus.options[mintus.selectedIndex].textContent);
110
110
 
111
- console.log(mintus.value);
112
-
113
- const result1 = d;
111
+ const result1 = new Date(d.getTime());
114
112
 
115
113
  result1.setMinutes(m-result1.getMinutes());
116
114