-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
38 lines (30 loc) · 1.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var imgURLs = ['images/heidi.jpg', 'images/heidiImg.jpg', 'images/nature.jpg', 'images/rose.jpg'];
function imgLoad(url) {
var executor = function (resolve, reject) {
var xhrReq = new XMLHttpRequest();
xhrReq.open('GET', url);
xhrReq.onload = function(response) {
if(xhrReq.status === 200) {
resolve(url);
} else {
reject('image is not loaded ');
}
};
xhrReq.onerror = function(){
reject('There was a network error.');
};
xhrReq.send();
};
var promise = new Promise(executor);
return promise;
}
window.onload = function () {
for(var i = 0; i < imgURLs.length; i++) {
imgLoad(imgURLs[i]).then(function (responseUrl) {
console.log('response ' + responseUrl);
var imgEle = document.createElement('img');
imgEle.src = responseUrl;
document.getElementById('imgContainer').appendChild(imgEle);
});
}
};