views:

109

answers:

2

I set up a slide show (Slideshow()) using setTimeout, and it works fine. I need to limit the slide show to 3 repeats, but when I add a while loop (Count()) it it prints Test 1 and stalls

function SlideShow()
{
  setTimeout("document.write('Test 1')", 1500);
  setTimeout("document.write('Test 2')", 3000);
  setTimeout("document.write('Test 3')", 4500);
}

function Count()
{
  var i=0;

  do
  {
    SlideShow();
    i++;
  }
  while (i<=3);
}
A: 

This works for me:

    function slideShow() {
        setTimeout("alert('1');", 1500); 
        setTimeout("alert('2');", 3000); 
        setTimeout("alert('3');", 4500); 
    }

    function count() {
        for(var i=0; i<3; i++) {
            slideShow();
        }
    }

    count();
Matt Williamson
(Google Chrome)
Matt Williamson
Thanks for the effort - it actually repeats the alert1 3 times, then alert2 3 times, etc. - Sorry - Rhys
Rhys
What is that you wanted? That's what you described above *"I need to limit it to 3 repeats"*
Matt Williamson
Sorry to be obscure - what I asked about is a slide show, repeated 3 times, not the same picture shown 3 times, then a new set of pics.... - Rhys
Rhys
A: 

You could just use one timeOut like in:

<img id="theImg" />
<script>
    var cnt = 2,
        i = 0,
        pics = [
            'image1.png',
            'image2.png',
            'image3.png'
        ];
    function SlideShow(ap){
        if(ap[i]){
            //set the src of theImg to the item i of the array
            document.getElementById('theImg').src = ap[i++];
            setTimeout(function(){
                SlideShow(ap);
            }, 1500);
        }else if(cnt--){
            i = 0;
            SlideShow(pics);
        }
    }
    SlideShow(pics);
</script>
Mic
Hey - That's brilliant, works a treat
Rhys
I need to add a string containing a tween routine after the timer (at }, 1500);). I have the routines inside variables var1, var2 etc.something like "tween='var' + i; echo tween;".The syntax seems to be wrong, because it won't run.If I manualy enter a Tween string it works fine, but I need to match each routine to the appropriate slide pic.
Rhys
Got it going, used another array for the tweens
Rhys