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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:40:19+00:00 2026-05-20T11:40:19+00:00

I want to call some functions but waiting for the previous one has finished.

  • 0

I want to call some functions but waiting for the previous one has finished.
I know jQuery provides a callback argument in several functions, but I want to learn how implement this behaviour in my own jQuery plugin. So this is the case:

After read answers from my previous question I wrote this:

    (function(callback){
        $('#art1').animate({'width':'1000px'},1000);
        callback();
    })((function(callback2){
        $('#art2').animate({'width':'1000px'},1000);
        callback2();
    })(function(){
        $('#art3').animate({'width':'1000px'},1000);
    }));

But not working. I’ve tried again and wrote this:

    animate1(function(){
        animate2(function(){
            animate3();
        })
    });

    function animate1(callback){
        $('#art1').animate({'width':'1000px'},1000);
        callback();
    }
    function animate2(callback){
        $('#art2').animate({'width':'1000px'},1000);
        callback();
    }
    function animate3(callback){
        $('#art3').animate({'width':'1000px'},1000);
        callback();
    }

But still not working. Three animates still starting at same time. I want they were called one after other. But without using:

    $('#art1').animate({'width':'1000px'},1000,'linear',function(){
        $('#art2').animate({'width':'1000px'},1000,'linear',function(){
            $('#art3').animate({'width':'1000px'},1000);        
        });        
    });  
  • 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-20T11:40:19+00:00Added an answer on May 20, 2026 at 11:40 am

    That’s what the Buffer is for:

    var Buffer = function(handler) {
        var tasks = [];
        // empty resolved deferred object
        var deferred = $.when();
    
        // handle the next object
        function handleNextTask() {
            // if the current deferred task has resolved and there are more tasks
            if (deferred.isResolved() && tasks.length > 0) {
                // grab a task
                var task = tasks.shift();
                // set the deferred to be deferred returned from the handler
                deferred = handler(task);
                // if its not a deferred object then set it to be an empty deferred object
                if (!(deferred && deferred.promise)) {
                    deferred = $.when();
                }
                // if we have tasks left then handle the next one when the current one 
                // is done.
                if (tasks.length > 0) {
                    deferred.done(handleNextTask);
                }
            }
        }
    
        // appends a task.
        this.append = function(task) {
            // add to the array
            tasks.push(task);
            // handle the next task
            handleNextTask();
        };
    };
    

    Then we just create a task handler for the animation, an animation task and append the tasks to the buffer.

    function handleAnimation(task) {
        var def = $.Deferred();
        $(task.id).animate(task.props, task.timeout, task.options function() {
            def.resolve();
        });
        return def.promise();
    }
    
    function AnimationTask(id, props, timeout, options) {
        this.id = id;
        this.props = props;
        this.timeout = timeout;
        this.options = options;
    }
    
    var buffer = new Buffer(handleAnimation);
    buffer.append(new AnimationTask("#art1", {
        "width": "1000px"
    }, 1000, "linear");
    buffer.append(new AnimationTask("#art2", {
        "width": "1000px"
    }, 1000, "linear");
    buffer.append(new AnimationTask("#art3", {
        "width": "1000px"
    }, 1000, "linear");
    

    This uses the magic of jQuery deferred to run tasks one after the other. It’s probably a lot easier to just chain them.

    Also this would work:

    (function(callback){
        $('#art1').animate({'width':'1000px'},1000, function() {
             callback();
        });
    })((function(callback2){
        $('#art2').animate({'width':'1000px'},1000, function() {
             callback2();
        });
    })(function(){
        $('#art3').animate({'width':'1000px'},1000);
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call a url with passing some parameter and i want to
I want to call subroutines in Perl like: sub temp { ---- some code
Actually I have some MVC3 Applications and I want to call this applications from
I'm using CoreLocation,and I want to call a method to do some computations on
I'm getting some very wierd errors. The compiler seems to want to call the
I want to create some kind of AJAX script or call that continuously will
I know it's possible to call the calling function, but is it possible to
I'm making calls to C functions from R using R's .Call interface. Some of
I want to call a webservice using google closures, via jsonp since i am
I want to call a html page on click of a button in mvc.

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.