teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

再修正した

2021/05/30 04:08

投稿

takaaa
takaaa

スコア7

title CHANGED
File without changes
body CHANGED
@@ -24,14 +24,14 @@
24
24
  カウントダウンを出しているviewにTime.currentを置いたのですが、毎秒とることができないので詰まっています。
25
25
  どなたかご教授お願いします。
26
26
 
27
- ##変更点
27
+ ## 再修正
28
28
 
29
29
  ```
30
30
  // start_timeとcurrent_timeはviewから取っています。
31
31
  const start_time = new Date(document.getElementById('start_time').dataset.time);
32
+ const countdown = () => {
32
33
  const current_time = new Date(document.getElementById('current_time').dataset.time);
33
34
  var diff = start_time - current_time;
34
- const countdown = () => {
35
35
  if (diff <= 0) {
36
36
  alert('セール開始です!');
37
37
  }

1

変更点の追記

2021/05/30 04:08

投稿

takaaa
takaaa

スコア7

title CHANGED
File without changes
body CHANGED
@@ -22,4 +22,29 @@
22
22
  ```
23
23
 
24
24
  カウントダウンを出しているviewにTime.currentを置いたのですが、毎秒とることができないので詰まっています。
25
- どなたかご教授お願いします。
25
+ どなたかご教授お願いします。
26
+
27
+ ##変更点
28
+
29
+ ```
30
+ // start_timeとcurrent_timeはviewから取っています。
31
+ const start_time = new Date(document.getElementById('start_time').dataset.time);
32
+ const current_time = new Date(document.getElementById('current_time').dataset.time);
33
+ var diff = start_time - current_time;
34
+ const countdown = () => {
35
+ if (diff <= 0) {
36
+ alert('セール開始です!');
37
+ }
38
+ const diff_sec = Math.floor(diff / 1000) % 60;
39
+ const diff_min = Math.floor(diff / 1000 / 60) % 60;
40
+ const diff_hour = Math.floor(diff / 1000 / 60 / 60) % 24;
41
+ const diff_day = Math.floor(diff / 1000 / 60 / 60 / 24);
42
+ document.getElementById('min').textContent = String(diff_min).padStart(2, '0');
43
+ document.getElementById('sec').textContent = String(diff_sec).padStart(2, '0');
44
+ document.getElementById('hour').textContent = String(diff_hour).padStart(2, '0');
45
+ document.getElementById('day').textContent = String(diff_day).padStart(2, '0');
46
+ diff --;
47
+ setTimeout(countdown, 1000);
48
+ }
49
+ countdown();
50
+ ```