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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:48:02+00:00 2026-06-02T23:48:02+00:00

I’m working on a webapp that uses jQuery, and I’m getting something that looks

  • 0

I’m working on a webapp that uses jQuery, and I’m getting something that looks like it could be a race condition when testing it. Despite my best efforts, though, I’m uncertain how to proceed.

The code is on GitHub, but I’ll summarize the odd behavior here.

Early AJAX Request

I’ve got some code that requires an AJAX call and for the DOM to be ready. To speed things up, I’m making the AJAX call before the $(document).ready() event fires. In the linked JS file, this is the “init” function. This function calls _build_site_select, which sets up the AJAX request. When complete, _on_site_list_success is called.

On $(document).ready()

When I get to ready(), I make the calls that require both the AJAX and the ready() event to be fired. In the linked code, this is the populate_site_select call. Since the AJAX might not be complete when ready() is called, I check a variable that I set after the AJAX operation is complete, and use setTimeout to delay the call to populate_site_select if it is still too early to complete. The variable site_list_ready is set when the AJAX call is complete, and is checked in functions that require the site list to be completely finished before executing.

Expected and Observed behavior

The behavior I expect to see is that populate_site_select will only be called after the object/associative array in _on_site_list_select is completely populated. However, this is not always the case. Sometimes, when I get to populate_site_select, the array is only partially populated – it seems consistent in the way that it fails (it’s always the same single entry in the array, as opposed to the ~30 or so that I expect).

What I’ve tried

I’ve also tried adding a setTimeout call to the _on_site_list_success call, which further separates the population of the array from the setting of the site_list_ready variable. However, this doesn’t prevent the problem.

I know that the JSON data returned from the AJAX call is correct (I can view it in the chrome debugger), and if I check the value of the site list variable later, (after the page has finished loading) it’s properly populated.

This doesn’t seem that unusual of a scenario, and I find it hard to believe that the bug is in Chrome (although I haven’t tested this with other browsers). What am I doing wrong?

  • 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-02T23:48:05+00:00Added an answer on June 2, 2026 at 11:48 pm

    I didn’t try the code below, but jQuery’s Deferred objects can help you.

    var domReady, ajax;
    domReady = jQuery.Deferred();
    
    jQuery(function () {
        // on DOM ready
        domReady.resolve();
    });
    
    ajax = jQuery.ajax({
      url: '/path/to/file',
      type: 'GET',
      dataType: 'xml/html/script/json/jsonp'
    });
    
    jQuery.when(domready, ajax).then(doneCallbacks, failCallbacks);
    

    Basically, you have two deferred objects, one that handles the DOM ready state, and the second that handles the AJAX call; we manually manage the first object, resolving it when the DOM is ready; the second one is automagically managed by jQuery itself ($.ajax objects are Deferred objects, and are resolved when the AJAX call succeeds).

    Finally, with jQuery.when() we observe the status of the deferred objects, and when they’re both resolved, the doneCallback is fired; if one of the two objects is rejected, the failCallback is fired.

    So, the doneCallbacks is the function that will be called after the two Deferred objects are resolved, so that the DOM is ready and the AJAX call has been made and succeeded.

    See the documentation for jQuery.when() and for the Deferred objects

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.