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 4592584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:33:35+00:00 2026-05-21T22:33:35+00:00

I have written a little script that randomly shuffles a series of divs –

  • 0

I have written a little script that randomly shuffles a series of divs – this works as expected (or hoped).

My problem is in the implementation. I want the divs to fade out, to be shuffled and to fade in again. What I have found is that the function moveBox() executes concurrently with any animation. I have tried calling the it as a callback function to all the elements (fadeOut, delay and fadeIn) in the animation, but always with the same effect – the shuffling and redistribution of the divs happens during the animation, and is therefore visible.

I have a solution (var ts=timeOut…) which makes the shuffle happen while the divs are hidden, but I am not convinced that this is the best solution.

I would like to know how to control the order of execution of functions and whether they should execute concurrently or in sequence.
My code :

<style>
    .tester{
        float:left;
        width:100px;
        height:100px;
        margin:5px;
        }
    .tester p{text-align:center;
        margin-top:20px;
    }
    .one{background-color:red;}
    .two{background-color:yellow;}
    .three{background-color:teal;}
    .four{background-color:blue;}
    .five{background-color:green;}
    .six{background-color:silver;}
</style>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function(){
        var anibase=jQuery(".tester").length;
        jQuery(".tester").fadeOut(1000);
        function moveBox(){
            function shuffle(){
                for (i = 0; i <= (anibase - 2); i++) {
                    mover = Math.floor(Math.random() * (anibase - i));
                    if (mover != 0) {
                        mover = mover + i;
                        jQuery(".tester:eq(" + mover + ")").insertBefore(".tester:eq(" + i + ")");
                    };
                };
            };
            jQuery(".tester").fadeOut(1500).delay(500).fadeIn(1500);
            var ts = setTimeout(shuffle,1500);
            var t=setTimeout(moveBox,5000);
        };
        moveBox();
    });

</script>
<body>
    <div class="tester one">
        <p>box 1</p>
    </div>
    <div class="tester two">
        <p>box 2</p>
    </div>
    <div class="tester three">
        <p>box 3</p>
    </div>
    <div class="tester four">
        <p>box 4</p>
    </div>
    <div class="tester five">
        <p>box 5</p>
    </div>
    <div class="tester six">
        <p>box 6</p>
    </div>
</body>

thanks in advance

  • 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-05-21T22:33:35+00:00Added an answer on May 21, 2026 at 10:33 pm

    JQuery has the ability to nest functions, effectively controlling the order of execution.

    First off although it is valid to nest functions in Javascript in this case it is not necessary, consider the following code:

    <script type="text/javascript">
    
      jQuery(document).ready(function(){
        moveBox();  //call the function that will do the work and then setTimeout
      });
    
    
    
      function moveBox(){
        jQuery(".tester").fadeOut("slow",function(){
          shuffle();
          $(this).fadeIn("slow");
          return;
        });
        var t=setTimeout(moveBox,5000);
      }
    
      function shuffle(){
        var anibase=jQuery(".tester").length;
        for (i = 0; i <= (anibase - 2); i++) {
          mover = Math.floor(Math.random() * (anibase - i));
          if (mover != 0) {
            mover = mover + i;
            jQuery(".tester:eq(" + mover + ")").insertBefore(".tester:eq(" + i + ")");
          }
        }
      }
    </script>
    

    The important part here to look at is this:

    jQuery(".tester").fadeOut("slow",function(){
      shuffle();
      $(this).fadeIn("slow");
      return;
    });
    

    This will instruct jQuery to

    1. Fade Out the .tester element
    2. When the fade out is complete execute shuffle()
    3. Then fade the elements back in

    You can see further examples of this in the fadeOut documentation, some of the examples show chained events

    Also by keeping your function definitions separate it makes it much easier to read.

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

Sidebar

Related Questions

I have written this little script to find executables that are passed as arguments
I have written a small little script that looks up a work on wikipedia
I have written a little script that reads every hour from a website and
I have written a little script in Python that I use to append text
I have written a little bash script that reads commands (one per line), in
Greetings. I have written a little python script that calls MySQL in a subprocess.
have written this little class, which generates a UUID every time an object of
I have written a little console application that uses s#arp. I can create an
I am learning flex and have written a little app with a TextArea that
I have a self-written Java application - a little mail monitor. It works with

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.