SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making slideshow only loop 5 times instead of continuously

    I'm using a Sitepoint slideshow script and I only want the slideshow to go through five times rather than looping forever. How do I change the code? Thanks!

    Code:
    var timer = 3;
    
    var photos = [
        ['pic1', 'Galaxy UGC 10214 without APEX method applied'],
        ['pic2', 'Applying the APEX method to the Tadpole galaxy'],
        ['pic3', 'Micro-cantilever experiment'],
        ['pic4', 'holder image'],
        ['pic5', 'holder image']
    ];
    
    var img, count = 1;
    
    function startSlideshow()
    {
      img = document.getElementById('photo');
      window.setTimeout('cueNextSlide()', timer * 1000);
    }
    
    function cueNextSlide()
    {
      var next = new Image;
    
      next.onerror = function()
      {
        alert('Failed to load next image');
      };
    
      next.onload = function()
      {
        img.src = next.src;
        img.alt = photos[count][1];
    
        img.width = next.width;
        img.height = next.height;
    
        if (++count == photos.length) { count = 0; }
    
        window.setTimeout('cueNextSlide()', timer * 1000);
      };
    
      next.src = 'public_affairs/images/slideshow/' + photos[count][0] + '.jpg';
    }
    
    addLoadListener(startSlideshow);
    
    function addLoadListener(fn)
    {
      if (typeof window.addEventListener != 'undefined')
      {
        window.addEventListener('load', fn, false);
      }
      else if (typeof document.addEventListener != 'undefined')
      {
        document.addEventListener('load', fn, false);
      }
      else if (typeof window.attachEvent != 'undefined')
      {
        window.attachEvent('onload', fn);
      }
      else
      {
        var oldfn = window.onload;
        if (typeof window.onload != 'function')
        {
          window.onload = fn;
        }
        else
        {
          window.onload = function()
          {
            oldfn();
            fn();
          };
        }
      }
    }
    Last edited by stymiee; Dec 12, 2006 at 10:51.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    loop flag

    Hi pcorey, welcome to the forums,
    The easiest way I can think of is to use a "flag" variable. eg "count".
    Every time the loop executes, increment the flag's value. Test for it's value in the loop. eg.
    if(count <5) doloop();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •