Solved

jQuery: Load images sequentially

Posted on 2011-09-23
4
428 Views
Last Modified: 2012-06-27
I want to load images sequentially from an array.  An image should not begin loading until the image before it has fully loaded.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

var images2Load = new Array(); 
images2Load[1] = 'http://upload.wikimedia.org/wikipedia/commons/c/ce/Pink_Lady_Slipper_flower_LNoyes.jpg';
images2Load[2] = 'http://upload.wikimedia.org/wikipedia/commons/7/7b/Pic-du-midi-dossau.jpg';
images2Load[3] = 'http://upload.wikimedia.org/wikipedia/commons/6/69/AiguillesArves001.jpg';


// Append first image
// After first image has fully loaded, load next image
// Do this for each item in the array
// Alert DONE! when all images have been loaded


});
</script>
<style type="text/css">
</style>
</head>
<body>
<div id="content"></div>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36589873
Test page : http://jsfiddle.net/PqN5v/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    var images2Load = new Array(); 
    images2Load[0] = '???? NO PIC ????';
    images2Load[1] = 'http://upload.wikimedia.org/wikipedia/commons/c/ce/Pink_Lady_Slipper_flower_LNoyes.jpg';
    images2Load[2] = 'http://upload.wikimedia.org/wikipedia/commons/7/7b/Pic-du-midi-dossau.jpg';
    images2Load[3] = 'http://upload.wikimedia.org/wikipedia/commons/6/69/AiguillesArves001.jpg';

    function nextImage() {
        var toload = images2Load[ $("img","#content").length + 1 ];
        if( toload != null ) {
            alert("go!");
            $("<img/>").attr({ "src":toload }).appendTo("#content");
            $("img", "#content").load(nextImage);
        }
    }
    
    nextImage();

});
</script>
<style type="text/css">
</style>
</head>
<body>
<div id="content"></div>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36589976
better :
$(document).ready(function() {

    var images2Load = new Array(); 
    images2Load[0] = '???? NO PIC ????';
    images2Load[1] = 'http://upload.wikimedia.org/wikipedia/commons/c/ce/Pink_Lady_Slipper_flower_LNoyes.jpg';
    images2Load[2] = 'http://upload.wikimedia.org/wikipedia/commons/7/7b/Pic-du-midi-dossau.jpg';
    images2Load[3] = 'http://upload.wikimedia.org/wikipedia/commons/6/69/AiguillesArves001.jpg';

    function nextImage() {
        var toload = images2Load[ $("img","#content").length + 1 ];
        if( toload != null ) {
            alert("go!");
            $("<img/>").attr({ "src":toload }).appendTo("#content").load(nextImage);
        }
    }
    
    nextImage();

});

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 36590002
Thanks!
0
 
LVL 16

Author Comment

by:hankknight
ID: 36656079
Here is a slightly modified version that I will use:
// Thanks, leakim971
var images2Load = new Array(); 
images2Load[1] = 'http://upload.wikimedia.org/wikipedia/commons/c/ce/Pink_Lady_Slipper_flower_LNoyes.jpg';
images2Load[2] = 'http://upload.wikimedia.org/wikipedia/commons/7/7b/Pic-du-midi-dossau.jpg';
images2Load[3] = 'http://upload.wikimedia.org/wikipedia/commons/6/69/AiguillesArves001.jpg';
images2Load[4] = 'http://upload.wikimedia.org/wikipedia/commons/6/69/AiguillesArves001.jpg';

$('body').prepend('<div id="preload"></div>')
function nextImage() {
	var toload = images2Load[ $("img","#preload").length + 1 ];
        if( toload ) {
            $('<img alt="" />').attr({ "src":toload }).appendTo("#preload").load(nextImage);
        }
	else {
	alert('Loaded');
	}
}
nextImage();

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now