Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jQuery: Load images sequentially

Posted on 2011-09-23
4
Medium Priority
?
477 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
[X]
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
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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)
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…
Suggested Courses

609 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