geeking
geeking

Reputation: 89

Load sequencing pictures

I have some pictures I want them to have to organize the page every 10 seconds.

As follows:

, ...

Until we finish all photos.

my problem :

method load don't work & i don't know method for waiting

Here is my code:

<html>
<head>
    <title></title>
    <script type="text/javascript" src='jq.js'></script>
</head>
<body>
    <div></div>
    <script type="text/javascript">
        var images=["images/0.jpg",
                    "images/1.jpg",
                    "images/2.jpg",
                    "images/3.jpg",
                    "images/4.jpg",
                    "images/5.jpg",
                    "images/6.jpg",
                    "images/7.jpg",
                    "images/8.jpg",
                    "images/9.jpg",
                    "images/10.jpg"
                ];
        for (var i=0;i<images.length;i++){
            var imgtag = '<img src='+images[i]+' />';
            $(imgtag).load(function() {
                $('div').append(imgtag);
                //Wait 10 sec   ==> Is there a function to Waiting :-??
            }
       }
    </script>
</body>
</html>

Thanks for your response.

Upvotes: 2

Views: 145

Answers (2)

Yanick Rochon
Yanick Rochon

Reputation: 53536

First, you need to wait for the DOM ready event, then use a recursive callback :

$(function() {   // on DOM ready

var images = [
    "images/0.jpg",
    "images/1.jpg",
    "images/2.jpg",
    "images/3.jpg",
    "images/4.jpg",
    "images/5.jpg",
    "images/6.jpg",
    "images/7.jpg",
    "images/8.jpg",
    "images/9.jpg",
    "images/10.jpg"
];

(function loadImg(index) {
    if (index >= images.length) return;  // no more image to load

    $('#divId').append( $('<img />').attr('src', srcImg).load(function() {
        // invoke this function again in 10 secs incrementing index by 1
        setTimeout(function() { loadImg(index + 1); }, 10000);
    }) );

})(0);   // initiate loading sequence with first image index

});

Upvotes: 1

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

Use setInterval function.

var images=["images/0.jpg",
                    "images/1.jpg",
                    "images/2.jpg",
                    "images/3.jpg",
                    "images/4.jpg",
                    "images/5.jpg",
                    "images/6.jpg",
                    "images/7.jpg",
                    "images/8.jpg",
                    "images/9.jpg",
                    "images/10.jpg"
                ];
var count = 0;
function loadImage() {
  if (count < images.length) {
    var imgtag = '<img src='+images[count]+' />';
    $('div').append(imgtag);
    count++;
  }
}

setInterval(loadImage, 10000);

Upvotes: 0

Related Questions