回答編集履歴

1

プログラム追加

2019/08/09 01:48

投稿

yxt003
yxt003

スコア184

test CHANGED
@@ -9,3 +9,79 @@
9
9
  timeLimitをテンプレート側で表示させたいからだと思いますが、計算に使うtimeLimitは
10
10
 
11
11
  ミリ秒かつnumberを保持する必要があるので、計算用の変数をもう一つ用意すればどうでしょうか
12
+
13
+
14
+
15
+ ```JavaScript
16
+
17
+ data() {
18
+
19
+ return {
20
+
21
+ words: [
22
+
23
+ 'apple', 'orange', 'banana'
24
+
25
+ ],
26
+
27
+ word: '',
28
+
29
+ location: 0,
30
+
31
+ score: 0,
32
+
33
+ miss: 0,
34
+
35
+ timeLimit: 3 * 1000,//表示用
36
+
37
+ timeLimitForCalc: 0,//計算用
38
+
39
+ startTime: 0,
40
+
41
+ isPlaying: false,
42
+
43
+ }
44
+
45
+ },
46
+
47
+ methods: {
48
+
49
+ updateTimer: function () {
50
+
51
+ const timeLeft = this.startTime + this.timeLimitForCalc - Date.now();
52
+
53
+
54
+
55
+ this.timeLimit = (timeLeft / 1000).toFixed(2);
56
+
57
+ const timeoutId = setTimeout(() => {
58
+
59
+ this.updateTimer()
60
+
61
+ }, 10)
62
+
63
+ if (timeLeft <= 0) {
64
+
65
+ clearTimeout(timeoutId);
66
+
67
+ }
68
+
69
+
70
+
71
+ },
72
+
73
+ startGame: function () {
74
+
75
+ this.isPlaying = !this.isPlaying
76
+
77
+ this.startTime = Date.now()
78
+
79
+ this.timeLimitForCalc = this.timeLimit //初期値をセット
80
+
81
+ this.updateTimer()
82
+
83
+ },
84
+
85
+ //以下略
86
+
87
+ ```