Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax load HTML content with image, how to check whether all images are loaded?

I'm doing a script for infinite scroll for my site:

$(window).scroll(function(){
  if($(window).scrollTop() == $(document).height() - $(window).height()){
    ...
    $.ajax({
      ...
      success: function(json) {if(json.success){
        ...
        // here I append an Ajax @response with a new element to my existing HTML
        $("#mindIndexSummaryFirst").append(json.items1);
        $("#mindIndexSummarySecond").append(json.items2);
        $("#mindIndexSummaryThird").append(json.items3);
        // here I try to check whether all images are loaded
        $("#mindIndexSummary img").on('load', function(){
          // and I need to call two functions on loaded element
          // hoverImg() need the loaded image to calculate the height
          $('.pi-hover-img'+json.course).hoverImg();
          $(".popup3").tooltip();
        });
      }
    ...
  });

A loaded HTML element is this:

<div class="page-item pi-hover-img  pi-normal">
  <a class="overlayPop" href="#">
  <a class="item_uri" id="course153" href="#">
    <img src="/public/file/course/image/153-course.png" class="pi-item-image">
  </a>
  <a href="#" class="popup5 hide-text" style="display: none;">
    <img src="/public/file/account/image/282-user.jpeg" class="item_image">
  </a>
  <a class="popup action-button big_interested " id="course_153" href="javascript:;" style="top: -162.5px; left: 83.5px; display: none;"> Mi interessa</a>
  <a href="javascript:;">Description</a>
  <a class="popup4 hide-text" href="javascript:;">Preview</a>
  <span class="popup2" style="display: none;">1€</span>
  <a href="/course/153/gestire-un-pubblico">
    <h2 style="top: 320px;">Gestire un pubblico</h2>
    <span class="rfloat pi-icon">
      <img src="/public/img/site_icon/video_course_black.png">
    </span>
    <div class="clearfix"></div>
  </a>
</div>

I load 5 elements, 15 images, but there is a possibility that fewer elements are loaded.

The problem is that my function was called more than one time (I test this with alert) and I'm not sure secure that the function was called after image load.

If someone had the same situation please suggest me something (P.S I apologize for my bad English).

like image 421
Artur Mamedov Avatar asked May 10 '13 15:05

Artur Mamedov


1 Answers

$("#mindIndexSummary img").on('load', function(){ });

Will attach the handler to each of the images that match the query - so each image will fire the function when it is loaded - not once when all are finished loading.

You would have to keep track of the images that have loaded in order to say whether they have all loaded.

Along the lines of:

var noOfImages = $("#mindIndexSummary img").length;
var noLoaded = 0;

$("#mindIndexSummary img").on('load', function(){ 

    noLoaded++;
    if(noOfImages === noLoaded) {
        handleAllLoaded();
    }

});
like image 137
Tom Elmore Avatar answered Sep 24 '22 21:09

Tom Elmore