views:

107

answers:

1

Hi. I am trying to get my own image slider to auto play with an interval of 4000 milliseconds. I've tried using setTimeOut but I cannot seem to get it to work. I would appreciate it if someone could help me out. Here is example of the slider:

http://www.matthewruddy.com/slider-intervalissue/

Any ideas how I can get it to auto play?

Here is the jQuery code: // Javascript Document

$(document).ready(function(){
var images = $('.slider li');
var slides = images.length;
var sliderwidth = 500;
var currentimage = 0;
var containerwidth =  sliderwidth*slides;
var autoplay = 4000;

$('.slider').css('width', containerwidth);

$('#next').bind('click',nextimage);

function nextimage(){
if(currentimage<slides-1){
currentimage++;
$('.slider').animate({left:-sliderwidth*(currentimage)},{duration:800,easing:'easeInOutCubic'});
}
else{
currentimage = 0;
$('.slider').animate({left:0},{duration:800,easing:'easeInOutCubic'})
}
};

});

Thanks, Matthew

A: 

I was able to set up an autoplay that runs quite nicely, using a combination of setTimeout and recursion.

I gave the nextimage() function a parameter autoplay, which is a boolean. Then I added the following line at the end of the function:

if (autoplay) { setTimeout(function() { nextimage(true); }, 4000); }

Finally I added this line at the end of the script, to get the autoplay loop going:

setTimeout(function() { nextimage(true); }, 4000);

Check out the demo here, constructed mostly from what you provided, with a few modifications to do the autoplaying: http://jsfiddle.net/bVjkP/

In short, you pass a boolean into the nextimage() function, to tell it whether or not to begin an autoplay. Then you simply call an initial setTimeout to get the ball rolling. The trick with this method is that you have to call an anonymous function through the setTimeout, which calls the nextimage function with autoplay set to true, because you can't pass parameters when calling a function with setTimeout. This is similar to the method for binding functions to events such as click or hover using jQuery.

Ender
Thank you! Your method works perfectly however I'm still somewhat don't understand it (I like to understand things regardless of whether or not they work).Why do you have to write the line twice? Why doesn't the first line;>if (autoplay) { setTimeout(function() { nextimage(true); }, 4000); } Get the autoplaying going?
Matthew Ruddy
That line will only keep the function going every 4 seconds after it is called the first time (assuming it's called with autoplay=true). You need something to initiate the loop in the first place. Think of it like pushing a slinky over the edge of a staircase. It'll keep going once you set it in motion, but you have to give it an initial push to make it start.
Ender
Ok I understand now! Thanks a million!
Matthew Ruddy
If you like my answer, could you please upvote and/or accept the answer? Thanks!
Ender
Hi Ender. Just wanted to point out that this would cause the timer to halve its speed when I clicked the 'next' button. However I managed to sort this out myself. Thanks again!
Matthew Ruddy
Yes, you're correct. You would would to change the binding on the next button to pass in false when calling nextimage, so that it wouldn't start a new chain of autoplaying. Alternately, you could cease autoplaying when the next button is clicked (this requires keeping track of the timerID). See here for an example of the first: http://jsfiddle.net/bVjkP/1/ and here for an example of the second: http://jsfiddle.net/bVjkP/2/ You have lots of options by combining setTimeout() and clearTimeout() creatively.
Ender