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

  • Home
  • SEARCH
  • 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 8844791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:34:32+00:00 2026-06-14T11:34:32+00:00

I’m playing with $.when and deferred to control flow of several functions. I need

  • 0

I’m playing with $.when and deferred to control flow of several functions. I need a little clarity on why something isn’t working and how it should be coded to work.

The end objective for this contrived example can be reached with inline callbacks but that is not the solution I’m looking for as each individual function is more complex than the example. The final code should do the following:

  • animate box 2
  • wait a second
  • animate box 3 after box 2 has finished and there has been a brief pause
  • wait a second
  • then animate box1 after box 3 has finished and there has been a brief pause
  • Finally alert saying everything is complete after everything is complete

I have tried many variations of this but I will try and present the ones that cause me the most grief. a frustrating fiddle found here

I began with the easiest piece, alerting a message after the 3 boxes where finished animating:

var a = $('.box1').animate({top: '100px'}, 2000),
    b = $('.box2').animate({top: '100px'}, 1000),
    c = $('.box3').animate({top: '100px'}, 2000);

$.when( a, b, c )
    .done( function(){ alert( 'all done' )} );​

no problem… I actually started to think I knew what I was doing…NOPE!

I next figured, I should be able to separate each variable into it’s own respective function… but that made the alert happen first, and then the animations reached their end abruptly by ignoring their animation duration time!

function a(){
    $('.box1').animate({top: '100px'}, 2000);
}
function b(){
    $('.box2').animate({top: '100px'}, 1000);
}
function c(){
    $('.box3').animate({top: '100px'}, 2000);
}

a();
b();
c();
$.when( a(), b(), c() )
    .done( alert('all done') );

Seems like a scope thing? So what if I move the $.when into the respective function to call the next one in line… again in the real world there is a lot more happening than just a single elements animation.

That didn’t work! why exactly? the alert pops up first again! huh? and then all the boxes animate quicker than specified to their final positions:

function a(){
    a = $('.box1').animate({top: '100px'}, 2000);
    $.when( a ).done( alert( 'all boxes moved down' ) );
}
function b(){
    b = $('.box2').animate({top: '100px'}, 1000);
    $.when( b ).done( c() );
}
function c(){
    c = $('.box3').animate({top: '100px'}, 2000);
    $.when( c ).done( a() );
}

b();

By jsfiddle update 100 I’m on stack overflow…but wait I figured, lets make it even simpler, remove all the $.when from each function and just add one after I call b() to fire off c()… unfortunately b() and c() fired simultaneously! What am I missing here?

function a(){
    a = $('.box1').animate({top: '100px'}, 2000);
}
function b(){
    b = $('.box2').animate({top: '100px'}, 1000);
}
function c(){
    c = $('.box3').animate({top: '100px'}, 2000);
}      

$.when( b() ).done( c() );

Never mind the fact that I was thinking I could

$.when( b() ).delay(1000).done( c() );

I’m trying like crazy to understand this when, done, and deferred stuff (which I can post more on if needed :)!) After watching an hour long jquery developer presentation on the stuff I thought I could implement it for this situation… In the end I have a simple task, and am obviously missing something! I would appreciate any direction / explanation offered.

html and css

<div class='boxes'>
    <div class='box box1'></div>
    <div class='box box2'></div>
    <div class='box box3'></div>
</div>​

.box{width: 40px; height: 40px; background-color: green; position:absolute;}
.box1{left: 0px;}
.box2{left: 60px;}
.box3{left: 120px;}
  • 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-14T11:34:33+00:00Added an answer on June 14, 2026 at 11:34 am

    When you started wrapping your animations in functions, you need to make sure that the function returns an object that contains a promise method. for example,

    function a() {
        return $('.box1').animate({top: '100px'}, 2000);
    }
    

    now you can do:

    $.when( a(), b(), c() ).done(function() {
        alert("all done!");
    });
    

    Now you can split that to control the order:

    // run b and wait one second, when done run c and wait 1 second, then run a
    b().delay(1000).promise().done(function(){
        c().delay(1000).promise().done(a);
    });
    

    Since you are never dealing with more than one promise at a time, you don’t need $.when at all.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.