Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:41:36+00:00 2026-06-15T02:41:36+00:00

Let me preface this by saying I am new to Javascript, but not to

  • 0

Let me preface this by saying I am new to Javascript, but not to programming. I call fadeOut on a jQuery Object like such:

$(x).fadeOut('fast');

I have other things that will be fading out and in, but I need them to wait for each other, and I do not necessarily know how many are going to be doing this. x is a string from an array that contains the items that are are fading in or out. In my first attempt I used the callback function in fadeOut like this:

$(x).fadeOut('fast', function(){ foo(modifiedArray); })

Where foo is the method I want, and modifiedArray is the array minus x. This still didn’t make it wait though, so next I tried:

$(x).fadeOut('fast');
while( $(x).css('display') != 'none' ){}
foo(modifiedArray);

But the loop never ends. How can I make the animation wait before I call foo(modifiedArray) again?

EDIT: Here is the full code

function animateElements(elements){
if (elements.length == 1){
    var x = elements[0];
    $(x).fadeIn('slow');
}
else{
    var x = elements.pop();
    $(x).fadeOut('fast');
    while(  $(x).css('display') != 'none' ){}
    animateElements(elements);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:41:38+00:00Added an answer on June 15, 2026 at 2:41 am

    For the sake of completeness. In your code you can fix the following things.

    1. Use .fadeIn() instead of .fadeOut().

      $(x).fadeIn('slow');
        ---^---
      
    2. You may want to use .shift() instead of .pop() to go over the array from left to right, not from right to left.

      var x = elements.pop();
                    ---^---
      
    3. Call the recursive step in the complete callback of .fadeOut() avoiding to check compulsively the element’s style.

      $(x).fadeOut('fast');
        ---^---
      while(  $(x).css('display') != 'none' ){}
      animateElements(elements);
      
    4. Use elements.length == 0 as a base case. This will improve readability.

      if (elements.length == 1) {
        --^--
      

    At the end, the code will look like this:

    function animateElements(elements) {
      if (elements.length) {
        var x = elements.shift();
        $(x).fadeOut('fast', function(){
          animateElements(elements);
        });
      }
    }
    

    See it live.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First let me preface this question by saying that I'm fairly new to Javascript.
Let me preface this by saying I am extremely new to git but have
Let me preface this by saying, i'm pretty new to rails and programming. I'm
First, let me preface this by saying that I'm brand-new to Ember.js and relatively
So let me preface this by saying that I'm not an SQL wizard by
Let me preface this by saying I'm very new to C# and Visual Studio
Let me preface this by saying that I'm pretty new to Java. I have
Let me preface this by saying this is not an actual situation of mine
Let me preface this by saying I am really new to C++. I want
Let me preface this question with saying that I am very new to Orchard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.