質問編集履歴
4
訂正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
JavaScript:
|
1
|
+
JavaScript:setIntervalで関数呼び出しをしても実行されない
|
body
CHANGED
File without changes
|
3
更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
7
|
-
Mozilla Thimbleというウェブ上でJavaScriptを書けるページで書いているのですが、
|
7
|
+
Mozilla Thimbleというウェブ上でJavaScriptを書けるページで書いているのですが、
|
8
8
|
```
|
9
9
|
Uncaught TypeError: Cannot read property 'gs_1' of undefined
|
10
10
|
TypeError: Cannot read property 'gs_1' of undefined
|
@@ -20,7 +20,7 @@
|
|
20
20
|
<script type="text/javascript"> </script>
|
21
21
|
|
22
22
|
<script>
|
23
|
-
var time=
|
23
|
+
var time=5;
|
24
24
|
|
25
25
|
var score=0;
|
26
26
|
var click=0;
|
@@ -38,10 +38,10 @@
|
|
38
38
|
document.g_switch.gs_4.value = r4;
|
39
39
|
document.g_switch.gs_5.value = r5;
|
40
40
|
|
41
|
-
function Life(){ //この関数
|
41
|
+
function Life(){ //この関数のif(time<=0)が実行されない
|
42
42
|
//document.getElementById("t").innerHTML = (t:"time");
|
43
43
|
stopper=0;
|
44
|
-
|
44
|
+
time--;
|
45
45
|
|
46
46
|
document.g_switch.gs_1.value = Math.floor(Math.random ( ) * 100)+1;
|
47
47
|
document.g_switch.gs_2.value = Math.floor(Math.random ( ) * 100)+1;
|
@@ -49,28 +49,30 @@
|
|
49
49
|
document.g_switch.gs_4.value = Math.floor(Math.random ( ) * 100)+1;
|
50
50
|
document.g_switch.gs_5.value = Math.floor(Math.random ( ) * 100)+1;
|
51
51
|
|
52
|
-
if(time<=0){
|
52
|
+
if(time<=0){ //ここがうまく実行されない
|
53
|
-
window.alert("aaa")
|
53
|
+
window.alert("aaa")
|
54
54
|
document.write('最大数クリック回数:');
|
55
55
|
document.write(click);
|
56
56
|
document.write('取得ポイント:');
|
57
57
|
document.write(score);
|
58
58
|
}
|
59
59
|
}
|
60
|
-
setInterval(Life,1000);
|
60
|
+
setInterval(Life,1000); //おそらくこれも機能していない
|
61
61
|
|
62
62
|
|
63
|
-
function point(x){
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
63
|
+
function point(x){
|
64
|
+
|
65
|
+
if(x>=document.g_switch.gs_1.value && x>=document.g_switch.gs_2.value && x>=document.g_switch.gs_3.value && x>=document.g_switch.gs_4.value && x>=document.g_switch.gs_5.value && stopper==0){
|
66
|
+
click++;
|
67
|
+
score+=x;
|
68
|
+
stopper++;
|
69
|
+
}else{
|
70
|
+
score-=x;
|
71
|
+
stopper++;
|
72
|
+
}
|
73
|
+
|
74
|
+
}
|
75
|
+
|
74
76
|
|
75
77
|
</script>
|
76
78
|
<form name = "g_switch">
|
2
更新
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
JavaScript:onclickで関数呼び出しをしても実行されない
|
1
|
+
JavaScript:onclickで関数呼び出しをしても上手く実行されない
|
body
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
初心者で現在JavaScriptの勉強をしているものです
|
2
2
|
### 前提・実現したいこと
|
3
|
-
<input type="button" onclick = "Life()" value="Start" style="widht:50px; height: 50px; font-size:20px;">で実装されているボタンをクリックすると関数Lifeを実行するようにしたい
|
3
|
+
<input type="button" onclick = "Life()" value="Start" style="widht:50px; height: 50px; font-size:20px;">で実装されているボタンをクリックすると関数Lifeを実行するようにしたいです。5つのボタンには乱数がそれぞれ割り当てられてはいるのですが、変数timeないしはsetInterval(Life,1000);がうまく動いてくれずに困っております。
|
4
4
|
|
5
5
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
7
|
-
Mozilla Thimbleというウェブ上でJavaScriptを書けるページで書いているのですが、
|
7
|
+
Mozilla Thimbleというウェブ上でJavaScriptを書けるページで書いているのですが、S
|
8
8
|
```
|
9
|
-
Uncaught
|
9
|
+
Uncaught TypeError: Cannot read property 'gs_1' of undefined
|
10
|
-
|
10
|
+
TypeError: Cannot read property 'gs_1' of undefined
|
11
|
-
at
|
11
|
+
at https://mozillathimblelivepreview.net/bramble/production/dist/vfs/.anonymous/projects/900bfa2a-d30d-4e6c-97f0-e5f6980865ee/index.html:1904:25
|
12
12
|
```
|
13
13
|
といったエラーが出てきます
|
14
14
|
### 該当のソースコード
|
15
15
|
|
16
16
|
```Javascript
|
17
17
|
<html>
|
18
|
-
<head>
|
18
|
+
<head>
|
19
|
-
|
19
|
+
<h1 id="t"></h1>
|
20
20
|
<script type="text/javascript"> </script>
|
21
21
|
|
22
22
|
<script>
|
@@ -26,11 +26,11 @@
|
|
26
26
|
var click=0;
|
27
27
|
var stopper=0;
|
28
28
|
|
29
|
-
var r1=(Math.random ( ) * 100)+1;
|
29
|
+
var r1=Math.floor(Math.random ( ) * 100)+1;
|
30
|
-
var r2=(Math.random ( ) * 100)+1;
|
30
|
+
var r2=Math.floor(Math.random ( ) * 100)+1;
|
31
|
-
var r3=(Math.random ( ) * 100)+1;
|
31
|
+
var r3=Math.floor(Math.random ( ) * 100)+1;
|
32
|
-
var r4=(Math.random ( ) * 100)+1;
|
32
|
+
var r4=Math.floor(Math.random ( ) * 100)+1;
|
33
|
-
var r5=(Math.random ( ) * 100)+1;
|
33
|
+
var r5=Math.floor(Math.random ( ) * 100)+1;
|
34
34
|
|
35
35
|
document.g_switch.gs_1.value = r1;
|
36
36
|
document.g_switch.gs_2.value = r2;
|
@@ -38,28 +38,29 @@
|
|
38
38
|
document.g_switch.gs_4.value = r4;
|
39
39
|
document.g_switch.gs_5.value = r5;
|
40
40
|
|
41
|
-
function Life(){ //この関数が実行されない
|
41
|
+
function Life(){ //この関数内のif(time<=0)が実行されない
|
42
|
-
document.getElementById("t").innerHTML = (t:"time");
|
42
|
+
//document.getElementById("t").innerHTML = (t:"time");
|
43
43
|
stopper=0;
|
44
44
|
time--;
|
45
45
|
|
46
|
-
document.g_switch.gs_1.value = (Math.random ( ) * 100)+1;
|
46
|
+
document.g_switch.gs_1.value = Math.floor(Math.random ( ) * 100)+1;
|
47
|
-
document.g_switch.gs_2.value = (Math.random ( ) * 100)+1;
|
47
|
+
document.g_switch.gs_2.value = Math.floor(Math.random ( ) * 100)+1;
|
48
|
-
document.g_switch.gs_3.value = (Math.random ( ) * 100)+1;
|
48
|
+
document.g_switch.gs_3.value = Math.floor(Math.random ( ) * 100)+1;
|
49
|
-
document.g_switch.gs_4.value = (Math.random ( ) * 100)+1;
|
49
|
+
document.g_switch.gs_4.value = Math.floor(Math.random ( ) * 100)+1;
|
50
|
-
document.g_switch.gs_5.value = (Math.random ( ) * 100)+1;
|
50
|
+
document.g_switch.gs_5.value = Math.floor(Math.random ( ) * 100)+1;
|
51
51
|
|
52
|
-
if(time<=0){
|
52
|
+
if(time<=0){ //実行されない
|
53
|
+
window.alert("aaa")//テスト
|
53
54
|
document.write('最大数クリック回数:');
|
54
55
|
document.write(click);
|
55
56
|
document.write('取得ポイント:');
|
56
57
|
document.write(score);
|
57
|
-
|
58
|
+
}
|
58
59
|
}
|
59
60
|
setInterval(Life,1000);
|
60
61
|
|
61
62
|
|
62
|
-
function
|
63
|
+
function point(x){ //この関数も実行されない
|
63
64
|
if(x>=document.g_switch.gs_1.value && x>=document.g_switch.gs_2.value && x>=document.g_switch.gs_3.value && x>=document.g_switch.gs_4.value && x>=document.g_switch.gs_5.value && stopper==0)
|
64
65
|
{
|
65
66
|
click++;
|
@@ -70,21 +71,21 @@
|
|
70
71
|
stopper++;
|
71
72
|
}
|
72
73
|
}
|
73
|
-
|
74
|
+
|
74
|
-
|
75
|
+
</script>
|
75
|
-
|
76
|
+
<form name = "g_switch">
|
76
|
-
|
77
|
+
|
77
78
|
<input type="button" onclick = "Life()" value="Start" style="widht:50px; height: 50px; font-size:20px;">
|
78
|
-
<input type="button" onclick = "
|
79
|
+
<input type="button" onclick = "point(document.g_switch.gs_1.value)" name="gs_1" value="SW1" style="widht:50px; height: 50px; font-size:20px;">
|
79
|
-
<input type="button" onclick = "
|
80
|
+
<input type="button" onclick = "point(document.g_switch.gs_2.value)" name="gs_2" value="SW2" style="widht:50px; height: 50px; font-size:20px;">
|
80
|
-
<input type="button" onclick = "
|
81
|
+
<input type="button" onclick = "point(document.g_switch.gs_3.value)" name="gs_3" value="SW3" style="widht:50px; height: 50px; font-size:20px;">
|
81
|
-
<input type="button" onclick = "
|
82
|
+
<input type="button" onclick = "point(document.g_switch.gs_4.value)" name="gs_4" value="SW4" style="widht:50px; height: 50px; font-size:20px;">
|
82
|
-
<input type="button" onclick = "
|
83
|
+
<input type="button" onclick = "point(document.g_switch.gs_5.value)" name="gs_5" value="SW5" style="widht:50px; height: 50px; font-size:20px;">
|
83
84
|
|
84
85
|
|
85
86
|
</form>
|
86
87
|
</head>
|
87
|
-
|
88
|
+
</html>
|
88
89
|
|
89
90
|
|
90
91
|
```
|
1
追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
document.g_switch.gs_4.value = r4;
|
39
39
|
document.g_switch.gs_5.value = r5;
|
40
40
|
|
41
|
-
function Life(){
|
41
|
+
function Life(){ //この関数が実行されない
|
42
42
|
document.getElementById("t").innerHTML = (t:"time");
|
43
43
|
stopper=0;
|
44
44
|
time--;
|
@@ -59,7 +59,7 @@
|
|
59
59
|
setInterval(Life,1000);
|
60
60
|
|
61
61
|
|
62
|
-
function switch(x){
|
62
|
+
function switch(x){ //この関数も実行されない
|
63
63
|
if(x>=document.g_switch.gs_1.value && x>=document.g_switch.gs_2.value && x>=document.g_switch.gs_3.value && x>=document.g_switch.gs_4.value && x>=document.g_switch.gs_5.value && stopper==0)
|
64
64
|
{
|
65
65
|
click++;
|