Search code examples
javascriptloopsfadeinfadeout

Loop FadeIn FadeOut Infinite


I need to repeat infinity this function does anyone can tell me a solution.

function AnimeRate(x, z, w){
    x.fadeIn(2000, function(){
        x.fadeOut(2000, function(){
            z.fadeIn(2000, function(){
                z.fadeOut(2000, function(){
                    w.fadeIn(2000);
                })
            })
        });
    });
}

Thanks


Solution

  • You can use .promise(), .then(); move AnimeRate function inside of .ready() handler; call .fadeOut(0) on w element before recursively calling AnimeRate

    $(document).ready(function() {
      function AnimeRate() {
        return w.fadeOut(0).promise().then(function() {
          return x.fadeIn(2000).promise().then(function() {
            return x.fadeOut(2000).promise().then(function() {
              return z.fadeIn(2000).promise().then(function() {
                return z.fadeOut(2000).promise().then(function() {
                  return w.fadeIn(2000).promise().then(function() {
                    this.fadeOut(0).promise().then(AnimeRate)
                  });
                })
              })
            })
          })
        })           
      }
    
      var x = $('.excellent');
      var z = $('.tresBon');
      var w = $('.bon');
      $(x, z, w).fadeOut(0);
      AnimeRate();
    });
    

    jsfiddle https://jsfiddle.net/9rn45k43/5/