回答編集履歴

1

sample

2016/12/15 09:58

投稿

yambejp
yambejp

スコア114843

test CHANGED
@@ -17,3 +17,65 @@
17
17
 
18
18
 
19
19
  ```
20
+
21
+
22
+
23
+ # sample
24
+
25
+ 以下だと、timerIdがどんどん上書きされるので、最後ものしかとめられない
26
+
27
+ ```javascript
28
+
29
+ <script>
30
+
31
+ var timerId;
32
+
33
+ timerId=setInterval(function(){document.getElementById('hoge1').value++},100);
34
+
35
+ timerId=setInterval(function(){document.getElementById('hoge2').value++},200);
36
+
37
+ timerId=setInterval(function(){document.getElementById('hoge3').value++},300);
38
+
39
+ setTimeout(function(){clearInterval(timerId)},1000);
40
+
41
+ </script>
42
+
43
+ <input type="text" id="hoge1" value="0"><br>
44
+
45
+ <input type="text" id="hoge2" value="0"><br>
46
+
47
+ <input type="text" id="hoge3" value="0"><br>
48
+
49
+
50
+
51
+ ```
52
+
53
+
54
+
55
+ 複数のタイマーをまわすならこんな感じ
56
+
57
+ ```javascript
58
+
59
+ <script>
60
+
61
+ var timerIds=[];
62
+
63
+ timerIds[0]=setInterval(function(){document.getElementById('hoge4').value++},100);
64
+
65
+ timerIds[1]=setInterval(function(){document.getElementById('hoge5').value++},200);
66
+
67
+ timerIds[2]=setInterval(function(){document.getElementById('hoge6').value++},300);
68
+
69
+ setTimeout(function(){timerIds.forEach(function(obj){clearInterval(obj)})},2000);
70
+
71
+ </script>
72
+
73
+ <input type="text" id="hoge4" value="0"><br>
74
+
75
+ <input type="text" id="hoge5" value="0"><br>
76
+
77
+ <input type="text" id="hoge6" value="0"><br>
78
+
79
+
80
+
81
+ ```