Skip to content

Commit 36ff6a0

Browse files
Resource loading: onload and onerror (#304)
* adds resource loading * edits resource loading * Update 2-ui/5-loading/03-onload-onerror/article.md * Update 2-ui/5-loading/03-onload-onerror/article.md * Update 2-ui/5-loading/03-onload-onerror/article.md * Update 2-ui/5-loading/03-onload-onerror/1-load-img-callback/task.md * Update 2-ui/5-loading/03-onload-onerror/1-load-img-callback/source.view/index.html * Update 2-ui/5-loading/03-onload-onerror/1-load-img-callback/solution.view/index.html Co-authored-by: Mykola Sopiha <[email protected]>
1 parent dc3c45c commit 36ff6a0

File tree

5 files changed

+91
-91
lines changed

5 files changed

+91
-91
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
The algorithm:
3-
1. Make `img` for every source.
4-
2. Add `onload/onerror` for every image.
5-
3. Increase the counter when either `onload` or `onerror` triggers.
6-
4. When the counter value equals to the sources count -- we're done: `callback()`.
2+
Алгоритм:
3+
1. Зробіть `img` для кожного джерела.
4+
2. Додайте `onload/onerror` для кожного зображення.
5+
3. Збільште лічильник, коли спрацьовує `onload` або `onerror`.
6+
4. Коли значення лічильника дорівнює кількості джерел, ми закінчили: `callback()`.

2-ui/5-loading/03-onload-onerror/1-load-img-callback/solution.view/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@
2121
}
2222
}
2323

24-
// ---------- The test ----------
24+
// ---------- Тест ----------
2525

2626
let sources = [
2727
"https://en.js.cx/images-load/1.jpg",
2828
"https://en.js.cx/images-load/2.jpg",
2929
"https://en.js.cx/images-load/3.jpg"
3030
];
3131

32-
// add random characters to prevent browser caching
32+
// додайте випадкові символи, щоб запобігти кешуванню браузера
3333
for (let i = 0; i < sources.length; i++) {
3434
sources[i] += '?' + Math.random();
3535
}
3636

37-
// for each image,
38-
// let's create another img with the same src and check that we have its width
37+
// для кожного зображення,
38+
// створімо інший img з тим же src і перевіримо, чи маємо його ширину відразу
3939
function testLoaded() {
4040
let widthSum = 0;
4141
for (let i = 0; i < sources.length; i++) {
@@ -46,7 +46,7 @@
4646
alert(widthSum);
4747
}
4848

49-
// should output 300
49+
// має вивести 300
5050
preloadImages(sources, testLoaded);
5151
</script>
5252

2-ui/5-loading/03-onload-onerror/1-load-img-callback/source.view/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77

88
<script>
99
function preloadImages(sources, callback) {
10-
/* your code */
10+
/* ваш код */
1111
}
1212

13-
// ---------- The test ----------
13+
// ---------- Тест ----------
1414

1515
let sources = [
1616
"https://en.js.cx/images-load/1.jpg",
1717
"https://en.js.cx/images-load/2.jpg",
1818
"https://en.js.cx/images-load/3.jpg"
1919
];
2020

21-
// add random characters to prevent browser caching
21+
// додайте випадкові символи, щоб запобігти кешуванню браузера
2222
for (let i = 0; i < sources.length; i++) {
2323
sources[i] += '?' + Math.random();
2424
}
2525

26-
// for each image,
27-
// let's create another img with the same src and check that we have its width immediately
26+
// для кожного зображення,
27+
// створімо інший img з тим же src і перевіримо, чи маємо його ширину відразу
2828
function testLoaded() {
2929
let widthSum = 0;
3030
for (let i = 0; i < sources.length; i++) {
@@ -35,7 +35,7 @@
3535
alert(widthSum);
3636
}
3737

38-
// every image is 100x100, the total width should be 300
38+
// кожне зображення має розмір 100x100, загальна ширина повинна бути 300
3939
preloadImages(sources, testLoaded);
4040
</script>
4141

2-ui/5-loading/03-onload-onerror/1-load-img-callback/task.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ importance: 4
22

33
---
44

5-
# Load images with a callback
5+
# Завантажте зображення з колбеком
66

7-
Normally, images are loaded when they are created. So when we add `<img>` to the page, the user does not see the picture immediately. The browser needs to load it first.
7+
Зазвичай зображення завантажуються під час їх створення. Тому, коли ми додаємо `<img>` на сторінку, користувач не відразу бачить зображення. Спочатку його потрібно завантажити браузеру.
88

9-
To show an image immediately, we can create it "in advance", like this:
9+
Щоб відразу показати зображення, ми можемо створити його "заздалегідь", наприклад:
1010

1111
```js
1212
let img = document.createElement('img');
1313
img.src = 'my.jpg';
1414
```
1515

16-
The browser starts loading the image and remembers it in the cache. Later, when the same image appears in the document (no matter how), it shows up immediately.
16+
Браузер починає завантажувати зображення і запам’ятовує його в кеші. Пізніше, коли те саме зображення з’являється в документі (незалежно від того, як), воно з’являється негайно.
1717

18-
**Create a function `preloadImages(sources, callback)` that loads all images from the array `sources` and, when ready, runs `callback`.**
18+
**Створіть функцію `preloadImages(sources, callback)`, яка завантажує всі зображення з масиву `sources` і, коли буде готова, запускає `callback`.**
1919

20-
For instance, this will show an `alert` after the images are loaded:
20+
Наприклад, після завантаження зображень буде відображатися `alert`:
2121

2222
```js
2323
function loaded() {
24-
alert("Images loaded")
24+
alert("Зображення завантажені")
2525
}
2626

2727
preloadImages(["1.jpg", "2.jpg", "3.jpg"], loaded);
2828
```
2929

30-
In case of an error, the function should still assume the picture "loaded".
30+
У разі помилки функція все одно повинна вважати картинку "завантаженою".
3131

32-
In other words, the `callback` is executed when all images are either loaded or errored out.
32+
Іншими словами, `callback` виконується, коли всі зображення завантажуються або є помилка.
3333

34-
The function is useful, for instance, when we plan to show a gallery with many scrollable images, and want to be sure that all images are loaded.
34+
Ця функція корисна, наприклад, коли ми плануємо показати галерею з великою кількістю зображень, які можна прокручувати, і хочемо бути впевненими, що всі зображення завантажені.
3535

36-
In the source document you can find links to test images, and also the code to check whether they are loaded or not. It should output `300`.
36+
У вихідному документі ви можете знайти посилання на тестові зображення, а також код, щоб перевірити, завантажені вони чи ні. Він має вивести `300`.

0 commit comments

Comments
 (0)