javascript - jQuery wait for image to load before looping -


this js , jq code :

for (i = 0; < data.length; i++) {     var $nitem = $('.search_result_template').clone().removeclass("search_result_template");     src = link_db + data[i]._id + "/" + readconfigurationsettings_img_upload_name()+"0";     $("<img/>")         .on('load', function () {             alert("image loaded correctly");             alert("src: " + src);             $nitem.find('.var_link_img').attr('href', src);         })         .on('error', function () {             alert("error loading image"); var src = readconfigurationsettings_link_default_img();             $nitem.find('.var_link_img').attr('href', src);         })         .attr("src", src);     $nitem.find('.var_link_img').text('image');     $nitem.attr("id", "search_result_template_" + i);     //alert($nitem.html());     $search_result.append($nitem); } 

the loop iterated before testing if image loaded or there error. thus, if have many elements src isn't correctly associated each image link.

how can wait for:

$("<img/>").on('load', function () {}); 

to executed can src $nitem.find('.var_link_img').attr('href', src);

the result of code shows first link(i==0) null , second one(i==1) src of precedent.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

mysql - FireDac error 314 - but DLLs are in program directory -