0

I have the following code where I get an xml file, and convert it into JSON:

showLoading();
$.get(data_file_path, {}, function(content, textStatus, jqXHR){
        data_object = jqXHR;
        jsonObject = xml2json.parser(data_object.responseText);         
        hideLoading();
    });

Where data_file_path is a .xml file. With this current code, the Loading .gif is not spinning because the xml2json.parser() call is not being doing asynchronously. This is simply a JavaScript Function. Nothing more, nothing less. How can I make this call ALSO Async so that the Loading .gif keeps spinning and then hides normally when the jsonObject is parsed?!

5
  • "With this current code, the Loading .gif is not spinning because the xml2json.parser() call is not being doing asynchronously" --- why do you think so? Your code looks fine and should work Commented May 24, 2012 at 2:04
  • @zerkms I think what he wants some kind of background threading to run the long working parser call on. Commented May 24, 2012 at 2:05
  • The gif appears... but is not making the circle motion. It is "frozen" while the xml2json.parser(data_object.responseText); function is being called... Commented May 24, 2012 at 2:12
  • @zerkms the browser doesn't get a chance to animate the .gif because it's too busy with the xml2json Commented May 24, 2012 at 2:14
  • @Jack should have the correct answer - if the image is showing (ie, showLoading() executed) and hideLoading() isn't executed until the callback function passed to $.get() is completed, then the browser is just busy with the code in the callback. There is nothing you can do to set the amount of CPU the browser will dedicate to running the callback; your only option is to optimize the code being executed. If you can provide JSON rather than XML, you can use jQuery.getJSON() instead... Commented May 24, 2012 at 2:20

1 Answer 1

1

The only reason AJAX is asynchronous in the first place is because you don't have to be running code constantly while the data is being fetched from the server, the script can just say "okay, when it's done, do this".

However, xml2json.parser is a JS script. It is constantly running code until it's done, so it cannot redraw.

The only way around this is to manually program your own version of it in order to have it load piece by piece on a timer.

Sign up to request clarification or add additional context in comments.

1 Comment

The xml2json.parser function, I assume, is simply converting the XML to JSON - there is no AJAX involved at all and is not the reason for the call being synchronous (which it isn't/shouldn't be - jQuery.get() is by default asynchronous).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.