$.each - wait for jSON request before proceeding

Posted by GaaayLooord on Stack Overflow See other posts from Stack Overflow or by GaaayLooord
Published on 2012-06-05T04:31:11Z Indexed on 2012/06/05 4:40 UTC
Read the original article Hit count: 111

Filed under:
|
|

I have an issue with the below code:

the jQuery.each is speeding on without waiting for the JSON request to finish. As a result, the 'thisVariationID' and 'thisOrderID' variables are being reset by the latest iteration of a loop before they can be used in the slower getJSON function.

Is there a way to make each iteration of the the .each wait until completion of the getJSON request and callback function before moving on to the next iteration?

$.each($('.checkStatus'), function(){
            thisVariationID = $(this).attr('data-id');
            thisOrderID = $(this).attr('id');
            $.getJSON(jsonURL+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&callback=?', function(data){
                if (data.response = 'success'){
                //show the tick. allow the booking to go through
                    $('#loadingSML'+thisVariationID).hide();
                    $('#tick'+thisVariationID).show();
                }else{
                //show the cross. Do not allow the booking to be made
                    $('#loadingSML'+thisVariationID).hide();
                    $('#cross'+thisVariationID).hide();
                    $('#unableToReserveError').slideDown();
                    //disable the form
                    $('#OrderForm_OrderForm input').attr('disabled','disabled');
                }
            })
        })

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JSON