javascript - Wait for images to load after creating them via an AJAX request -


i getting images (74 files) ajax request. loading them in array. change style calling function changemargins(). issue function called when images not yet loaded , ready.

imagearray = new array();       $.ajax({   url: url,   success: function (data) {     counter = 0     $(data).find("a").attr("href", function (i, val) {       if (val.match(/\.(jpe?g|png|gif)$/)) {          imagearray[counter]= new imageitem(url + val)         ++counter;         }     });      changemargins(imagearray); 

how can wait completion of ajax and load events on image elements , continue processing?

this problem best addressed "promisifying" image loading process, ie create promise resolves when images have loaded.

there's design decision made ... whether (a) swallow load errors or (b) allow individual error cause whole process fail.

assuming (a), write :

function loadimages() {     return $.ajax({         url: url,         // other ajax params?     }).then(function(data) {         return $.deferred(function(dfrd) { // promisify entire image loading proceess             var imagearray = [];             var counter = 0; // tally of images have either loaded or failed load             $(data).find('a').get().map(function(element) {                 // map elements in data array of hrefs                 return element.href;             }).filter(function (href) {                 // filter out non-jpe?g|png|gif                  return href.match(/\.(jpe?g|png|gif)$/);             }).foreach(function(val) {                 // each match, create image() object, push onto array, attach onload , onerror handlers, , set `src` attribute.                 var img = new image();                 imagearray.push(img);                 img.onload = function loadhandler() {                     counter += 1;                     if(counter == images.length) {                         dfrd.resolve(imagearray);                     }                 };                 img.onerror = function errorhandler() {                     console.log(new error('image ' + url + val + ' failed load'));                     // here, might choose splice failed images out of `imagearray`.                     counter += 1;                     if(counter == images.length) {                         dfrd.resolve(imagearray);                     }                 };                 img.src = url + val;             });         });     }).then(function(imagearray) {         changemargins(imagearray);         return imagearray;     }); } 

notes:

  • more typically, 1 choose promisify @ lowest level (ie each image() individually) promises expensive, therefore 74 images, it's better promisify en masse.
  • by promisifying, , returning promise loadimages(), caller informed of completion of process - can chain loadimages().then(...) , stuff when images have loaded/failed.

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -