How to make images did not loading in the same time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How to make images did not loading in the same time?

How to do this: 1st image: complete 2nd image: complete 3rd image: complete Not like this: 1st, 2nd, 3rd Image: Complete

27th Dec 2018, 11:48 PM
Jingga Sona
Jingga Sona - avatar
5 Answers
+ 18
Try it const loadImages = function(sources, callback){ let nb = 0; let loaded = 0; let imgs = {}; for(let i in sources){ nb++; imgs[i] = new Image(); imgs[i].src = sources[i]; imgs[i].onload = function(){ loaded++; console.log(`${loaded} complete`); if(loaded == nb){ callback(imgs); } } } }
28th Dec 2018, 12:49 AM
Lương Văn Tuấn
Lương Văn Tuấn - avatar
+ 4
Taste i think jingga is asking about image preloader lương tuấn good code👍 I am adding just in case jingga doesn't understand what sources and callback should be Jingga Sona 🐺 If i interpret your question correctly, you are going to make an image preloader, and the result will be something like this: ... I am sharing Taste's preloader demo for you (he coded for me back when I asked in Q&A) : Image Preloader example by Taste: https://code.sololearn.com/WCZaHjViG0Ve/?ref=app Below for enrichment only: If you want to eat "fast food", just use Taste's template. If you want to truly master it, check my scripts and see how I have varied the logic. To implement callback, it is not a must to have a function which takes another function as parameter, it is more about calling another function when something has ended. This one is a callback too: https://code.sololearn.com/WBJB498Ga91S/?ref=app Edit: I have taken my code away, because Calvin's demo for you is easy and clear.
28th Dec 2018, 1:05 AM
Gordon
Gordon - avatar
+ 2
Hide them all. Then show them one by one. Thats the only thing i can think of
27th Dec 2018, 11:57 PM
Taste
Taste - avatar
+ 2
Try to study this https://code.sololearn.com/Wrs20U62hU2s/?ref=app It loads images sequentially, suitable for loading image gallery with large numbers of images https://code.sololearn.com/WA55b8eiXb3T/?ref=app
28th Dec 2018, 4:05 AM
Calviղ
Calviղ - avatar
0
lúóng tuâń please go to my profile I need your help
9th Mar 2019, 6:57 PM
Aiden
Aiden - avatar