title: Return to First Slide after reaching Last Slide - with jQuery Slider
body: <p>I have a jQuery slider with an auto advance function. But I want to know how to return back to the first slide after the last slide is reached. The currentSlide variable (which holds the index of the current slide we are viewing) just keeps getting bigger and bigger, without returning to zero after the last slide is reached. I haven't included all of my jQuery script for the slider itself because that would be huge, and I think unnecessary. Here is the code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div class="slider" data-slide="0"&gt;
      &lt;div class="slide"&gt;&lt;/div&gt;
      &lt;div class="slide"&gt;&lt;/div&gt;
      &lt;div class="slide"&gt;&lt;/div&gt;
      &lt;div class="slide"&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript" src="js/jquery-1.7.2.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
  $(window).load(function(){
      var currentSlide = $(".slider").attr("data-slide"); /*Every time a slide is viewed, the index number of that slide is added to an html5 data- attribute. That way, we know what slide we are on. This var stores that information. */
      var timeOut = null;
  
      (function autoAdvance(){
          $('.sliderNav').find('.sliderTrigger' + (currentSlide)).trigger('click',[true]); /* Automatically click the trigger that will advance the slider to the next slide. */
          currentSlide++;
          timeOut = setTimeout(autoAdvance,5000);
      })();
  
  });
  
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>