views:

36

answers:

2

I'm trying to create a carousel effect that automatically cycles through each pictures every 3 seconds.

    $(".headline_img").each(function(intIndex){
        setTimeout($(this).show(),3000);
    });

The timeout delay is not working.

This shows all of the images instantly as soon as the dom loads. It's like its ignoring the setTimeout function.

Did I miss something?

Note: I'm calling this using $(document).ready, do you think that might effect it?

+1  A: 

You need to change the timeout for each one. Right now, you're attaching the same timeout to all of them at the same time. Something like this should work without changing your code much:

$(".headline_img").each(function(intIndex){
    setTimeout($(this).show(),3000 * (intIndex +1));
});

Refactoring to use queue might be more robust in the long term.

jball
thanks this helps for staging the shows in sequence. but it is still showing all of the images at once with no time delay.
dMix
Are the images hidden by default when the page loads and the jQuery doesn't run? Do you need to add a `$(this).hide();` before the `setTimeout`?
jball
yep they are all hidden
dMix
+3  A: 

The setTimeout function takes a function reference or a string. Your code calls the show method for each element immediately. I'm not sure if this will work:

$(".headline_img").each(function(intIndex){
    setTimeout($(this).show, 3000);
});

but it's worth a try...

Mike McCaughan
Didn't even notice that Mike, good call. Hopefully @dMix can confirm if that's the problem. The timeout will still need to be staggered for each element rather than being fixed at 3000.
jball
True. My eyes went right to the parens since I've been bitten by that particular problem many times. ;-)
Mike McCaughan