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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:26:55+00:00 2026-06-05T02:26:55+00:00

I have an issue with the below code: the jQuery.each is speeding on without

  • 0

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');
                }
            })
        })
  • 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-05T02:26:56+00:00Added an answer on June 5, 2026 at 2:26 am

    Two things two change for a quick fix:

    1. Use var before your variables to make them local instead of global. That way, you’ll get new variable instances for each of your iterations. Always use var unless you really intend on polluting the global namespace:

          var thisVariationID = $(this).attr('data-id');
          var thisOrderID = $(this).attr('id');
      
    2. Use == to compare instead of = 😉

          if (data.response == 'success'){
      

    Your code will most likely start working as intended after fixing these issues that you seem to have overlooked. But, if you’re willing to go with a more drastic change, you could also make use of Deferreds.

    To execute a bunch of fetches in parallel:

    var fetches = $('.checkStatus').map(function () {
        var thisVariationID = $(this).attr('data-id'),
            thisOrderID = $(this).attr('id');
    
        return $.getJSON(/* blah blah */);
    });
    
    fetches = $.when.apply($, fetches);
    
    fetches.done(function () {
        // all parallel fetches done
    });
    

    To serialize fetches:

    var fetching = $.when();
    
    $('.checkStatus').each(function () {
        var thisVariationID = $(this).attr('data-id'),
            thisOrderID = $(this).attr('id');
    
        fetching = fetching.pipe(function () {
            return $.getJSON(/* blah blah */);
        });
    });
    
    fetching.done(function () {
        // last fetch done
    });
    

    Overkill? Perhaps. But it’s flexible, and sometimes may be worth the syntactic sugar of having code that literally reads “when my fetches are done”.

    (Note: The above implementation is an optimistic implementation for illustration purposes only. In a real implementation you should also handle failures.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with this jquery code below. Please give me advice or
I have been facing issue with sphinx result set for string attribute. Below is
I have below code in Jquery $(document).ready(function() { // bind to cells with an
I have an issue in image validation using jQuery Or JavaScript. Please see code
I have an issue with php where code works on on computer but wont
I have a code as you can see below. I am trying to find
I have an issue with my javascript not waiting for the return of the
http://jsfiddle.net/yrM3H/2/ I have the following code: jQuery(document).ready(function() { jQuery(.toggle).next(.hidden).hide(); jQuery(.toggle).click(function() { $('.active').toggleClass('active').next('.hidden').slideToggle(300); $(this).toggleClass('active').next().slideToggle(fast); });
I have an issue with jQuery and Safari (5.03). Hope this makes sense. I
I have a jQuery code which works perfect on desktop browsers; $(span#checkbox_err).mouseout(function () {

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.