Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding div dynamically, but images very slow to load

I'm creating divs dynamically based on ajax callbacks. Each div contains a single png image:

var myDiv = "<div class='myClass' id='divid'>" + 
            "<img id='newDiagDivId' src='images/approved-icon.png'>" + 
            "<div style='display:inline-block;vertical-align:top;'>blah</div>" + 
            "</div>";

...here's how I add it:

$(myDiv).hide().appendTo( divContainer).fadeIn( 100);

The div displays correctly formatted, but the png image takes about 5-10s to show up. It's a tiny image, just 2kb, hosted locally from the app itself. The issue occurs on FF, Chrome, and IE, there's literally no difference in behavior.

The page handles about 2-3 ajax callbacks per second in "rapid fire" fashion, spitting out these divs for each callback, I don't have a slow machine, so I don't suspect the browser falling behind while loading the images.

Is there anything I can do to force the images to load faster or, immediately upon the div being added to the dom?

like image 691
raffian Avatar asked Aug 22 '13 15:08

raffian


1 Answers

Ok, so I'm answering my own question yet again...

Decided to preload images in the DOM based on this, works in FF, Chrome, IE!

$(window).load(
      function() { 
          preload(['images/approved-icon.png','images/denied-icon.png'])});

function preload(arrayOfImages) {
    $(arrayOfImages).each(function () {
        $('<img />').attr('src',this).appendTo('body').css('display','none');
    });
}
like image 68
raffian Avatar answered Oct 20 '22 22:10

raffian