I am cofused who I should use callbacks in this situation:
I and fire displayHighlights() function after highlightImages() is finished, so I don't need to use setTimeout()
First function is validation if images are not throwing errors, second is displaying only three of them.
<ul class="clear">
<li style="display : none" class="highlight-photos"><a class="highlight_photo"></a></li>
<li style="display : none" class="highlight-photos"><a class="highlight_photo"></a></li>
<li style="display : none" class="highlight-photos"><a class="highlight_photo"></a></li>
</ul>
function highlightImages() {
$(".highlight_photo").each(function() {
var fileName = $(this).data('url')
var image = new Image();
var that = $(this);
image.onerror = badImage;
image.src = fileName;
function badImage( event) {
var el = $(".highlight_photo[data-url='" + fileName+ "']");
el.parent().remove();
}
});
setTimeout(function(){displayHighlights();},500);
};
function displayHighlights() {
if ($(".highlight_photo").parent().length <= 3 ) {
$(".highlight-photos").show();
} else {
$("ul.clear li:lt(3)").addClass("visible");
$(".highlight-photos").not(".visible").remove();
$("ul.clear li:lt(3)").show();
}
}