Solved

jQuery: Load images sequentially

Posted on 2011-09-23
4
422 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

760 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

21 Experts available now in Live!

Get 1:1 Help Now