-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
81 lines (74 loc) · 2.04 KB
/
script.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
const createImage = function (imgPath) {
return new Promise(function (resolve, reject) {
const image = document.createElement('img');
image.src = imgPath;
image.addEventListener('load', () => resolve(image));
image.addEventListener('error', () => reject(new Error('Path Not Found')));
});
};
const wait = function (seconds) {
return new Promise(resolve => {
setTimeout(resolve, seconds * 1000);
});
};
let image;
/* createImage('./img/img-1.jpg')
.then(el => {
el.classList.add('images');
document.body.appendChild(el);
image = el;
return wait(2);
})
.then(() => {
image.style.display = 'none';
console.log('2 Seconds has passed');
return wait(2);
})
.then(() => {
return createImage('./img/img-2.jpg');
})
.then(el => {
el.classList.add('images');
document.body.appendChild(el);
image = el;
return wait(2);
})
.then(() => {
image.style.display = 'none';
console.log('2 Seconds has passed');
})
.catch(err => console.error(err)); */
const loadNPause = async function () {
try {
let imgElement = await createImage('./img/img-1.jpg');
document.body.append(imgElement);
imgElement.classList.add('images');
await wait(2);
imgElement.style.display = 'none';
await wait(2);
imgElement = await createImage('./img/img-2.jpg');
document.body.append(imgElement);
imgElement.classList.add('images');
await wait(2);
imgElement.style.display = 'none';
} catch (err) {
console.error(`Something went wrong : ${err.message}`);
}
};
//loadNPause();
const theDiv = document.querySelector('.images');
const loadAll = async function (imgArr) {
try {
const imgs = imgArr.map(img => createImage(img));
console.log(imgs);
const imgsEls = await Promise.all(imgs);
imgsEls.forEach(img => {
theDiv.append(img);
img.classList.add('parallel');
});
} catch (err) {
console.error(`Something went wrong : ${err.message}`);
}
};
loadAll(['./img/img-1.jpg', './img/img-2.jpg', './img/img-3.jpg']);