javascript - JQuery - Image Loop Wait for Loading -
i have following loop dynamically loads images onto page. small numbers of images, 10, works great. beyond that, start getting issues in loading images, remote server overwhelmed , not sending images (get red x's instead of images). images sent server imaging database, unfortunately overwhelmed.
for (var c = 0; c < b; c++) { url = transurl + "&action=getrelateditem&docid="+ d[c].id +"&side=front"; img = $("<img />").attr('src', url ).attr('width','600'); allimages.append(img); }
in page, allimages empty div on page. think need wait image load before moving on , trying load one, , on, i'm not sure how ask loop stop , wait image load before moving on next one?
thanks in advance.
update : per kevin, adjusted src assignment
i believe you're looking basic image preloader. tried adapt current code, believe using loop current needs wouldn't clean in terms of performance. assuming need append each image sequentially when done loading :
var count = 0; // initial count 0 allimages.on('insertimg', insertimgfn); // when event insert image triggered, run insertimgfn insertimgfn(); // runs function once start loading process << edit function insertimgfn(){ url = transurl + "&action=getrelateditem&docid="+ d[count].id +"&side=front"; img = new image(); // defines image object $(img).load(function(){ // on image load completion var countmatch = count < b; // checks if count less amount of images if(countmatch){ allimages.append(img); // appends image count++; // increase count allimages.trigger('insertimg'); // triggers event }// end of if countmatch });// end of .load img.src = url; // assigns url image object $(img).width(600); // sets width image };// end of insertimgfn
Comments
Post a Comment