0

I'm trying to do the next thing: when first part finished (including the function AccountClicked that gets details from the server), start the second part.

how can I do it please?

    // first part
    $('#accountsTable li input').each(function () {
        isGetLastSearchOperation = true;
        var accountId = $(this).attr('id');
        if ($.inArray(accountId, LastSearchedAccountsIds) > -1) {
            AreClickedButtonsEnable = true;
            $(this).attr('checked', true);
            AccountClicked(this);
        }
        // uncheck the other goals
        else {
            $(this).attr('checked', false);
        }
    });

    // second part
    $('#commercialGoalsTable li input').each(function () {
        isGetLastSearchOperation = true;
        var goalId = $(this).attr('id');
        if ($.inArray(goalId, LastSearchedGoalsIds) > -1) {
            AreClickedButtonsEnable = true;
            $(this).attr('checked', true);
            //CommercialGoalClicked(this);
        }
        // uncheck the other goals
        else {
            $(this).attr('checked', false);
        }
    });

any help appreciated!

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138

2 Answers2

3

The best solution would probably be to change up your code so you can send one ajax request.

Since AccountClicked() uses ajax and is async, I would first change that function to return a promise object, then do wait until all of the promises have been resolved like so:

var promises = [];

// first part
$('#accountsTable li input').each(function () {
    isGetLastSearchOperation = true;
    var accountId = $(this).attr('id');
    if ($.inArray(accountId, LastSearchedAccountsIds) > -1) {
        AreClickedButtonsEnable = true;
        $(this).attr('checked', true);
        promises.push(AccountClicked(this));
    }
    // uncheck the other goals
    else {
        $(this).attr('checked', false);
    }
});

$.when.apply(null, promises).done(function () {

    // second part
    $('#commercialGoalsTable li input').each(function () {
        isGetLastSearchOperation = true;
        var goalId = $(this).attr('id');
        if ($.inArray(goalId, LastSearchedGoalsIds) > -1) {
            AreClickedButtonsEnable = true;
            $(this).attr('checked', true);
            //CommercialGoalClicked(this);
        }
        // uncheck the other goals
        else {
            $(this).attr('checked', false);
        }
    });

});

A couple of simple options for returning a promise from a function:

function AccountClicked() {
    return $.ajax({
        // ...
    });
}

function AccountClicked() {
    var def = $.Deferred();

    $.ajax({
        // ...
    }).done(function() {
        def.resolve();
    });

    return def.promise();
}
Jason P
  • 26,984
  • 3
  • 31
  • 45
  • thank you but it doesn't work.. it entered the second part before the `accountclicked` calls finished.. – Alon Shmiel Mar 18 '14 at 17:20
  • Did you change `AccountClicked` to return a promise, and resolve the promise when the ajax function has completed? – Jason P Mar 18 '14 at 17:22
  • _"yes, I copied what you wrote.."_ - That seems to be a self-contradicting statement. My answer doesn't contain the code for the `AccountClicked()` function that you need to change. – Jason P Mar 18 '14 at 17:28
  • sorry, I didn't understand you.. how can I return a promise? `return promise;`? – Alon Shmiel Mar 18 '14 at 17:29
  • 1
    I updated my answer with a couple of simplified examples of returning a promise. – Jason P Mar 18 '14 at 17:32
  • This is a great answer and clear about how to add in a promise (with the examples) I took out the close vote because this is clearer. – Hogan Mar 18 '14 at 17:38
0

You could just add an if statement at the end of your first each loop that find the number of elements, when it reaches that high number, start the next each loop.

// first part
var numElem = $('#accountsTable li input').length;
$('#accountsTable li input').each(function (e) {//Notice the 'e' in this function for your index number
    isGetLastSearchOperation = true;
    var accountId = $(this).attr('id');
    if ($.inArray(accountId, LastSearchedAccountsIds) > -1) {
        AreClickedButtonsEnable = true;
        $(this).attr('checked', true);
        AccountClicked(this);
    }
    // uncheck the other goals
    else {
        $(this).attr('checked', false);
    }
    if(e>=numElem-1){
            // second part
        $('#commercialGoalsTable li input').each(function () {
            isGetLastSearchOperation = true;
            var goalId = $(this).attr('id');
            if ($.inArray(goalId, LastSearchedGoalsIds) > -1) {
                AreClickedButtonsEnable = true;
                $(this).attr('checked', true);
                //CommercialGoalClicked(this);
            }
            // uncheck the other goals
            else {
                $(this).attr('checked', false);
            }
        });
    }
});

EDIT :: Forgot zero position, edited the if statement to take care of that.

Here's a fiddle of an example http://jsfiddle.net/L4asQ/

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86