chudi
chudi

Reputation: 35

jQuery Ajax: How to wait to finish ajax in main script?

I have code:

$this.ajaxForm({
    beforeSend: function (form) {
        $('input[name *= "XXX"]').each(function (index) {
            $.GetJSON({
                url: "XXX",
                data: {
                    XXX: $(this).val()
                }
                success: function(json) {
                    if (json.error)
                    {
                    }
                }
            });
        }
        anotheraction();
    }
});

I would like to stop execution of next part of script (in loop or before "anotheraction") until end of the previous Ajax (of course if is json.error).

Edit: I would like break a loop (when i have json.error) and return false in beforeSend

Edit2: In theory async:false should help, but doesn't work too.

It is possible?

Upvotes: 0

Views: 167

Answers (2)

Dave Hogan
Dave Hogan

Reputation: 3221

Yes it's possible just by using the return keyword

Upvotes: 0

yves amsellem
yves amsellem

Reputation: 7244

If you want to delay an operation until a success call to the server, just include it in the success callback:

$.GetJSON({
    url: "XXX",
    data: {
        XXX: $(this).val()
    }
    success: function(json) {
        anotheraction();
        ...
    }
});

Upvotes: 2

Related Questions