回答編集履歴
3
追記日時修正
test
CHANGED
@@ -72,7 +72,7 @@
|
|
72
72
|
|
73
73
|
|
74
74
|
|
75
|
-
**(2015/12/02
|
75
|
+
**(2015/12/02 07:59追記)**
|
76
76
|
|
77
77
|
ひょっとして実行順も保証したいという意味だったでしょうか。
|
78
78
|
|
2
実行順を保証するコードを追記
test
CHANGED
@@ -71,3 +71,95 @@
|
|
71
71
|
```
|
72
72
|
|
73
73
|
|
74
|
+
|
75
|
+
**(2015/12/02 17:59追記)**
|
76
|
+
|
77
|
+
ひょっとして実行順も保証したいという意味だったでしょうか。
|
78
|
+
|
79
|
+
実行順を保証するコードを追記しました。
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
```JavaScript
|
84
|
+
|
85
|
+
'use strict';
|
86
|
+
|
87
|
+
(function () {
|
88
|
+
|
89
|
+
function handleLoad (event) {
|
90
|
+
|
91
|
+
var img = event.target,
|
92
|
+
|
93
|
+
imgList = this.imgList,
|
94
|
+
|
95
|
+
srcList = this.srcList;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
img.removeEventListener('load', this, false);
|
100
|
+
|
101
|
+
imgList[srcList.indexOf(img.getAttribute('src'))] = img;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
if (Object.keys(imgList).length === srcList.length) {
|
106
|
+
|
107
|
+
this.complete(imgList);
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
function next (imgList) {
|
116
|
+
|
117
|
+
for (var i = 0, l = imgList.length, img; i < l; ++i) {
|
118
|
+
|
119
|
+
img = imgList[i];
|
120
|
+
|
121
|
+
console.log(img, img.width, img.height);
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
function main () {
|
130
|
+
|
131
|
+
var srcList = ['sample1.jpg', 'sample2.jpg', 'sample3.jpg'],
|
132
|
+
|
133
|
+
listener = {
|
134
|
+
|
135
|
+
handleEvent: handleLoad,
|
136
|
+
|
137
|
+
srcList: srcList,
|
138
|
+
|
139
|
+
imgList: [],
|
140
|
+
|
141
|
+
complete: next
|
142
|
+
|
143
|
+
};
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
for (var i = 0, l = srcList.length, img; i < l; ++i) {
|
148
|
+
|
149
|
+
img = new Image;
|
150
|
+
|
151
|
+
img.addEventListener('load', listener, false);
|
152
|
+
|
153
|
+
img.src = srcList[i];
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
main();
|
162
|
+
|
163
|
+
}());
|
164
|
+
|
165
|
+
```
|
1
`load` イベントのサンプルを追記
test
CHANGED
@@ -2,6 +2,72 @@
|
|
2
2
|
|
3
3
|
が、`addEventListener` で `load` を監視したほうが素直ですし、負荷も小さいと思います。
|
4
4
|
|
5
|
-
- [HTML Standard](https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element)
|
5
|
+
- [4.8.4 The img element - HTML Standard](https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element)
|
6
6
|
|
7
7
|
- [HTMLImageElement - Web API インターフェイス | MDN](https://developer.mozilla.org/ja/docs/Web/API/HTMLImageElement)
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
**(2015/12/01 19:56追記)**
|
12
|
+
|
13
|
+
`load` イベントのサンプルを追記しました。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```JavaScript
|
18
|
+
|
19
|
+
'use strict';
|
20
|
+
|
21
|
+
(function () {
|
22
|
+
|
23
|
+
function handleLoad (event) {
|
24
|
+
|
25
|
+
var img = event.target;
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
img.removeEventListener('load', this, false);
|
30
|
+
|
31
|
+
this.complete(img);
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
function next (img) {
|
38
|
+
|
39
|
+
console.log(img.width, img.height);
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
function main () {
|
46
|
+
|
47
|
+
var srcList = ['sample1.jpg', 'sample2.jpg', 'sample3.jpg'],
|
48
|
+
|
49
|
+
listener = {handleEvent: handleLoad, complete: next};
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
for (var i = 0, l = srcList.length, img; i < l; ++i) {
|
54
|
+
|
55
|
+
img = new Image;
|
56
|
+
|
57
|
+
img.addEventListener('load', listener, false);
|
58
|
+
|
59
|
+
img.src = srcList[i];
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
main();
|
68
|
+
|
69
|
+
}());
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
|