Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know when images are done loading in AJAX response

I have a jQuery ajax function that loads some content into a div, some of the content is images. I would like to said until those images which where just loaded in my ajax, are finished loading, and THEN run a function, such as showing the content. This way, I won't have the content loaded into the div and the images start loading. I want them to be loaded, then put the content, or show() the content in the div.

I have seen many solutions to this, such as using .load(), but it does not seem to work for content that has been loaded using ajax.

like image 300
Nic Hubbard Avatar asked Nov 01 '09 20:11

Nic Hubbard


People also ask

How can I alert AJAX response data?

ajax({ url:"myscript. php", dataType: "json", success:function(data){ alert(data. cenas); } });

How do I know if AJAX request is successful?

You can check when exactly it returns "success" : // If successful, handle type chaining if ( status >= 200 && status < 300 || status === 304 ) { ... // If not modified if ( status === 304 ) { statusText = "notmodified"; ... // If we have data } else { try { ...

How display loading image or loader when AJAX call is in progress?

To display loader on ajax call use beforeSend and complete events.

How do I get AJAX response time?

The most simple method would be to add var ajaxTime= new Date(). getTime(); before the Ajax call and in the done get the current time to calculate how long the Ajax call took to make. var ajaxTime= new Date(). getTime(); $.


2 Answers

You need to add the load handler in the AJAX callback in order to have it apply to the images that are being loaded via the AJAX call. You may also want to have a timer set to show the content after some interval in case the images get loaded before the load handler is applied.

$.ajax({
     ...
     success: function(data) {
          var imageCount = $(data).filter('img').length;
          var imagesLoaded = 0;
          $(data).hide()
                 .appendTo('#someDiv')
                 .filter('img')
                 .load( function() {
                     ++imagesLoaded;
                     if (imagesLoaded >= imageCount) {
                        $('#someDiv').children().show();
                     }
                  });
          setTimeout( function() { $('#someDiv').children().show() }, 5000 );
      }
});
like image 164
tvanfosson Avatar answered Sep 28 '22 19:09

tvanfosson


This is an updated version of the code tvanfosson provided.

You have to make sure the imageCount variable is counting a node list and not a string as was the problem in the original code from I can deduce:

$.ajax({
    url      : 'somePage.html',  
    dataType : "html",
    success  : function(data) {

        $(data).hide().appendTo('#someDiv');

        var imagesCount = $('#someDiv').find('img').length;
        var imagesLoaded = 0;

        $('#someDiv').find('img').load( function() {
            ++imagesLoaded;
            if (imagesLoaded >= imagesCount) {
                $('#someDiv').children().show();
            }
        });

        var timeout = setTimeout(function() {
            $('#someDiv').children().show();
        }, 5000);
    }                     
});
like image 24
bgreater Avatar answered Sep 28 '22 21:09

bgreater