Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jQuery: Load images sequentially

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

Suggested Solutions

Title # Comments Views Activity
Html fieldset fix its height and width 4 30
jquery tab header text 1 23
jQuery force form POST 7 45
AJAX, If No returns don't let the AJAX Call run again 2 20
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

856 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