views:

335

answers:

2

I have this code to run a slideshow (this is only part of what really going in my code)

var slideshow = {
        delay: 5000,
        actions:[],
        run: function() {
                if (slideshow.actions.length) {
                        slideshow.actions.shift()();
                        setTimeout(slideshow.run, slideshow.delay);
                }
        }
};
 var tn;
$(".sideimg").each(function(){
        var that = this;

        slideshow.actions.push(function(){
 if (tn != "") {
  out(tn);
 }
 over($(that).attr("id"));
 var $paneTarget = $('#lyr1');

 var $target = $paneTarget.find('#'+$(that).attr("id"));
 $paneTarget.stop().scrollTo( $target , 800 );

 $("#rimg").fadeOut("slow",function () {
 $("#rimg").attr("src",$(that).attr("bsrc")); 
 $("#rimg").attr("alt",$(that).attr("alt")); 
   $("#rimg").fadeIn("normal");
  });
 tn = $(that).attr("id");

        });

});

after finishing running each sideimg class it stops... i need to re start from the beggining... i have tried to recall it - but nothing happend

thanks guys

+1  A: 

For this situation, I wouldn't use the each method at all. I'd use setTimeout, an array of object that have your image/href information, and a value that tells you where in the array of images you are. When you get to the end of the array, reset the value (the following code is simplified for clarity, and not tested):

var index = 0;
var images = // array of image containing objects here. 

function RunSlideShow()
{
     // modify the following line to perform any effects, etc, and other attribute settings you need. 
     $("#rimg").attr("src", images[index].image);

     index = index + 1;
     if (index = images.length)
         index = 0;
     window.setTimeout('RunSlideShow();', 5000);
}

RunSlideShow();

Each time the function in your slideshow object is called, you really need to have that function load the next slide before it exits. The issue with having each reloop when it's done, is the question: "When does it stop?" If you pre-load all the images, to continue forever, you'll cause an infinite loop and more than likely crash the browser. You need the same function to set the image, then prepare the next image to be shown, but it should look no further in the future than the next image. Anytime you try to go beyond that, you're at risk of creating an ever-growing collection of items that will never stop.

David Morton
i had a code like that, but the thing is, i have several thumbnails that i want to run over each and one of them and then enlarge the big on to display it in a container. while moving on each img (thumbnail) i am fadeing in and out, and scrolling to the right thumbnail on the scrollable thumbnails container.so after getting to the end of the each - i need it to repeat it self
Y.G.J
But how many times should it repeat itself? Forever? It can't do that, you'll lock the client's browser. How to solve this? Each time a mouseover event occurs on one of the images, call clearTimeout to clear the existing timeout, then set the "index" value to the newly hovered image, then call RunSlideShow to start the loop again. At that point, RunSlideShow should start back up where it left off.
David Morton
that's exactly what i had before when i used JSnow i want "only" Jquery, so i tought i could do that with each, for me and for a browser setTimeout for all of the images in the array is the same like running over each image with the same classboth should forever and will do the same for RAM, Browser, etc.is there a way to recall my each function again (in my original code)?
Y.G.J
+1  A: 

in you Run method you shift the array elements.. this removes them from the array.. you should shift it and then push it back in at the end ...

var slideshow = {
        delay: 5000,
        actions: [],
        timer: null,
        pause: function(){ clearTimeout( slideshow.timer ); },
        resume: function(){ slideshow.timer = setTimeout(slideshow.run, slideshow.delay); },
        run: function() {
                    if (slideshow.actions.length) {
                            var current = slideshow.actions.shift();
                            current();
                            slideshow.actions.push( current );
                            slideshow.timer = setTimeout(slideshow.run, slideshow.delay);
                    }
            }
};

I just added the timer variable which will hold the timeout object so you can clear it on demand, and also a couple of methods to start/stop the slideshow..

now just add hover events to the elements that you want to start/stop the slideshow with slideshow.pause() or slideshow.resume()

Gaby
is it possible to pause and continue that loop?
Y.G.J
on demand ? or at specific intervals ?
Gaby
on demand, i need to have it on mouse over on the thumbnails so the slideshow will stop and continue when mouse is out
Y.G.J
just added some more info to the code to handle this..
Gaby