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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:14:30+00:00 2026-06-17T16:14:30+00:00

I have 3 different jQuery functions like: function step1() { … } function step2()

  • 0

I have 3 different jQuery functions like:

function step1() {
  ...
}

function step2() {
  ...
}

function step3() {
  ...
}

and if I call them like this:

step1();
step2();
step3();

they will be all executed in the same time. How can I call them one by one so step2(); is called after step1(); is finished and step3(); is called after step2(); is finished.

// MORE INFO

Step1() is executing a json and appends details to html, Step2() is executing another json and appends info in the divs created by Step1() and Step3() simply hides a loading animation.

// THE FUNCTIONS

As requested by some users, here are the functions:

jQuery.noConflict();

jQuery(document).ready(function($) {

    var cardType = $.cookie('cardType');
    var categoryID = $.cookie('categoryID');

    function step1() {
        $.getJSON('http://domain.com/benefits/active/all.json', function(data) {
            $.each(data, function(i,item){
                if (item.benefit_type_id == categoryID) {
                    content = '<li><a class="showDetails" data-offer-description="' + item.description + '" data-offer-period="' + item.begin_date + ' - ' + item.end_date + '"><div class="company" title="' + item.business_id + '"></div><div class="shortdescription">' + item.name + '</div></a></li>';
                    $(content).appendTo("#thelist");
                }
            }); 
            step2();
        });
    } // function step1();

    function step2() {  
        $('.company').each(function(i, item) {
            var tempTitle = item.title;
            $.getJSON('http://domain.com/businesses/from_list/' + tempTitle + '.json', function(data2) {
                $.each(data2, function(i,item){
                    content = '<span>' + item.name + '</span>';
                    $(content).appendTo("div[title='" + tempTitle + "']");
                }); 
                step3();
            });
        });
    } // function step2();

    function step3() {
        loaded();
        $('#loading').delay(1000).fadeOut('slow', function() {
            // Animation complete.
        });
    } // function step3();

    step1();

});
  • 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-17T16:14:31+00:00Added an answer on June 17, 2026 at 4:14 pm

    Not knowing what you are executing in each function, I’m assuming none of them executes an asynchronous call themselves, i.e: ajax request and so on.

    Should you have additional asynchronous calls occur in each function the below would not apply as is.

    One option would be to use call one from the other similar to this:

    function step1() {
        //...
        step2();
    }
    
    function step2() {
        //...
        step3();
    }
    
    function step3() {
        //...
    }
    

    or you can use callbacks in the function signature, similar to this:

    //pass step2 function as parameter to step1
    //pass step3 function as parameter to step2
    step1(step2(step3));
    
    function step1(callback) {
        //...
        callback();
    }
    
    function step2(callback) {
        //...
        callback();
    }
    
    function step3() {
        //...
    }
    

    Or using jQuery deferred might work, similar to this:

    $.when($.when(step1()).then(step2)).then(step3);
    

    I’m not 100% sure on the deferred solution using .when().

    The code above seems to work (DEMO) using when() but as FelixKing mentioned in the comments if you update the methods to return a deferred promise you can do this:

    step1().then(step2).then(step3);
    

    DEMO – Using a deferred promise


    Each deferred object must be resolved() though for the next method
    to be executed.
    This would also give you some control if you have
    a scenario for example in which you don’t want to execute step3() if
    step2() fails to do something and calls .reject() for instance.

    Have a play around with the deferred objects in the fiddle and have a look at
    the deferred promise documentation too for more details.

    Sample-Code from DEMO:

    step1().then(step2).then(step3);
    
    function step1() {
        var $dfStep1 = new $.Deferred();
    
        console.log("step1");
    
        $dfStep1.resolve();
        return $dfStep1.promise();
    }
    
    function step2() {
        var $dfStep2 = new $.Deferred();
    
        console.log("step2");
    
        $dfStep2.resolve();
        return $dfStep2.promise();
    }
    
    function step3() {
        var $dfStep3 = new $.Deferred();
    
        console.log("step3");
    
        $dfStep3.resolve();
        return $dfStep3.promise();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have different php output in jQuery-based tabs. This output is formed from database
I have found and customized this JQuery script, which displays different content when different
I am using jQuery fullcalendar plugin , I have tried altering this many different
I have a jquery function(This is not written by me anyway still I am
I have two different functions bound to one element and event, basically they are
I'm developing win8(metro style) application with Html5-js-jquery. I have this code segment; GetBoutiqueDetail: function
I have different sentences which all have double quotes in them, like: <h3 class=myClass>Sentence
I have a bunch of Javascript functions that look like the following: function generateBusinessImage
I have a mysterious JS Problem: I activate different jQuery-Plugins with one function. It's
I have 3 different jquery uses & put it into one code as follows:

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.