0

I'm trying to get this script to loop, yet I can't.

$(document).ready(function inout() {
    $('#review1').fadeIn(1500).delay(3500).fadeOut(1500);
    $('#review2').delay(3500).fadeIn(1500).delay(3500)
     });
1
  • try add ; after $('#review2').delay(3500).fadeIn(1500).delay(3500) Commented Jul 14, 2012 at 5:26

1 Answer 1

2

If you want review1 to fadeIn, then fadeOut, then review2 to fadeIn, then fadeOut with delays between, you can do it like this;

$(document).ready(function inout() {
    function cycle() {
        $('#review1').fadeIn(1500).delay(3500).fadeOut(1500, function() {
            $('#review2').delay(3500).fadeIn(1500).delay(3500).fadeOut(1500, function() {
                setTimeout(cycle, 1500);
            });
        });
    }

    cycle();
});

This uses the jQuery animation completion function in order to link separate animations so one starts after the other completes. These are asynchonous animations so you can't just use normal sequential programming.

Sign up to request clarification or add additional context in comments.

2 Comments

How come it fades out without prompting it to? setTimeout just pauses the sequence right?
I modified my answer to make sure both review1 and review2 fadeIn, then fadeOut since that must be what you want if you want a repeating cycle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.