Execute automatically in a loop a function
<p>I have this slider where the gotoslide function changes the slide when the navigation links are pressed.
<a href="http://jsfiddle.net/AHYVr/" rel="nofollow">http://jsfiddle.net/AHYVr/</a></p>

<p>What is the way to make a loop where the gotoslide function autoruns from 0 to lastone and then goes back to start?</p>

<pre><code>var num_slides;
var slides;
var current;
var sa_auto = true;

jQuery(document).ready(function() {
  init_slide()
});

function init_slide(){
  slides = jQuery('.slides_container div');
        num_slides = (slides).length;
        pagination = '&lt;ul class="pagination"&gt;';
        var i=1;
        jQuery.each(slides, function() {
    pagination += '&lt;li&gt;&lt;a href="javascript:gotoslide('+i+')"&gt;'+i+'&lt;/a&gt;&lt;/li&gt;';
                i++;
  });
  pagination += '&lt;/ul&gt;';
  jQuery(pagination).insertBefore('#slides');


}

function gotoslide(n){
  sa_auto = false;
        showslide(n);
}

function showslide(n){
     current = n;
        var leftpos = (1-n)*700;
        pagination = jQuery('.pagination li')
     pagination.removeClass('current');
        jQuery(pagination[n-1]).addClass("current");
     slides.removeClass('current');
        jQuery(slides[n-1]).addClass("current");
  jQuery('.slides_container').animate({
    left: leftpos
  }, 2000);
}
</code></pre>

<p>I've tried to make a function that increases the gotoslide parameter by 1 and then use it with a setInterval, but it fails. This is the code:</p>

<pre><code> var t;
        function time (){
         if ( j &lt; num_slides ) {
               show_slide(j++);
         }
        };

        t= setInterval(time, 2000);
        clearInterval(t);
</code></pre>
