0

I am trying to test a fadein and fadeout function that is looping arround, with different content

    var next = null;
    var outer = $('.twitter');
    var current = outer.find('.twitterinner:first');
    current.fadeIn();
    function fade() {
        if (current.next('div.twitterinner').length > 0) {
            next = current.next('div.twitterinner');
        } else {
            next = outer.find('div.twitterinner:first');
        }

        current.fadeOut(500);
        setTimeout(function(){
            next.FadeIn(500);
        },1000);
//      next.fadeIn(500);
        current = next;
        setTimeout(fade, 2000);
    }
    fade();

and the html

<div class="col-xs-12 col-md-12 padding twitter">    
    <div class="twitterinner">Jellyfish Webdesign was afgelopen weekend aanwezig bij #YoutopiaArtsFestifal in het #Lloydhotel in A'dam! De website hebben we gesponsord.<br></div>
    <div class="twitterinner">Laat uw email adres achter via http://t.co/MHUXpcY1NE om op de hoogte te blijven van onze nieuwe website release. #Jellyfishux<br></div>
    <div class="twitterinner">Jellyfish Webdesign is vandaag officieel live gegaan! Voor meer informatie bezoek onze website, http://t.co/MHUXpcY1NE #jellyfishux<br></div>                
</div>

If i remove the setTimeout after the startup, it runs just fine (and i include again the fadeIn) but once I add the setTimeout with the fadein function in it, it stops working. I cant seem to find where I go wrong. Can anyone point me in the right direction?

5
  • 3
    setTimeout works a-synch-ron-ous-ly. Think about that :) Commented Nov 24, 2014 at 13:00
  • what if i told you that fade has a callback Commented Nov 24, 2014 at 13:02
  • 2
    FadeIn != fadeIn - jsfiddle.net/arunpjohny/poqga8w0/1 Commented Nov 24, 2014 at 13:03
  • i know, use fade then :) Commented Nov 24, 2014 at 13:10
  • Javascript is still new territorium for me sadly. Thanks for the great responds all. The fadeIn did it indeed for me. I am glad I see lots of alternative ways for this to solve, and to be honest, i never thought about using the setInterval. Commented Nov 24, 2014 at 13:17

5 Answers 5

1

I think you have to use setInterval() instead of setTimeout() as your function is getting call repeatidly. See below code

NOTE - please correct spelling for fadeIn(500), it starts with small f.

var next = null;
    var outer = $('.twitter');
    var current = outer.find('.twitterinner:first');
    current.fadeIn();
    function fade() {
        if (current.next('div.twitterinner').length > 0) {
            next = current.next('div.twitterinner');
        } else {
            next = outer.find('div.twitterinner:first');
        }

        current.fadeOut(500, function(){
           next.fadeIn(500);
            current = next;
        });
    }
   setInterval(fade,2000);

DEMO

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

Comments

1

If you want to execute something after an animation, you can do it like this (chaining)

current.fadeOut(500, function () {
    next.FadeIn(500);
    current = next;
});

To loop i suggest you to use interval instead of setTimeout.

setInterval(fade, 2000);

Comments

1

In "FadeIn", "f" should be small

Your Code :

setTimeout(function(){
        next.FadeIn(500);
    },1000);

Actual Code :

setTimeout(function(){
        next.fadeIn(500);
    },1000);

Comments

1

Try with setInterval

var i = 0;

setInterval(function() {
    $(".twitterinner").fadeOut("fast");
           $(".twitterinner").eq(i).fadeIn("slow");

     i++;
    if(i == $(".twitterinner").length){
       i = 0;   
    }


} ,1000);

DEMO

1 Comment

Set interval is a nice way (something that never crossed my mind to be honest. Had to do some read up on it), but I must point out in your code didnt supplied the timeout i wanted in between the fadeout and back in. They overlapped and that wasn't the meaning. I did thumbsup you though for the idea of setInterval.
1

You can use setInterval, but if you prefer to control it with a setTimeout, the better is to set just one timeout in the same time and chain your calls using callbacks.

Something like that :

function fade() {
    if (current.next('div.twitterinner').length > 0) {
        next = current.next('div.twitterinner');
    } 
    else {
        next = outer.find('div.twitterinner:first');
    }

    current.fadeOut(500, function() {
        // executed after 500ms
        next.delay(500).fadeIn(500, function() {
            // executed after 500 + 500 +500 = 1500 ms;
            current = next;
            setTimeout(fade, 500); // executed after 1500 + 500 = 2000 ms.
        });
    });
}

or if you want to have a more clearer script to execute all of this, you can define a function for each step (this is useful if your animation become more complex) :

function fade() {
    if (current.next('div.twitterinner').length > 0) {
        next = current.next('div.twitterinner');
    } 
    else {
        next = outer.find('div.twitterinner:first');
    }

    current.fadeOut(500, step2);

    function step2() {
        // executed after 500ms
        next.delay(500).fadeIn(500, step3);
    }

    function step3() {
        // executed after 500 + 500 +500 = 1500 ms;
        current = next;
        setTimeout(fade, 500); // executed after 1500 + 500 = 2000 ms.
    }
}

Comments

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.