1

i have a quick question. I want to iterate over all checked checkboxes and parse the json files for the checked ones.

Here is my problem: The $().each function is synchronous and the $.getJSON() function is asynchronous. So i know i would have to pause the exectuion of the each() loop until the JSON File is parsed. How can i pause the loop? Or any other ideas solving this problem? Thank you very much.

$("input:checkbox[name=check]:checked").each(function()
{
    $.getJSON( $(this).val(), function( data ) {
        //Do smth.
        //Pause the each loop?
    }).error(function(jqXhr, textStatus, error) {
            alert("ERROR: " + textStatus + ", " + error);
    });
}); 
alert("foo"); //<-- gets executed before all json files are parsed.
Nils
  • 25
  • 5
  • Why would you need to pause the each loop? What are you doing with data returned from server? – A. Wolff Apr 26 '14 at 15:10
  • The JSON Files contain questions. And i want to check how many questions in all selected json files are. So instead of alert("foo"); i want to check how many questions in the parsed json files are contained. Right now the each function is finished before even the first json file is completly parsed. – Nils Apr 26 '14 at 15:19
  • You should look into deferreds see this [example](http://stackoverflow.com/questions/6162855/jquery-deferred-with-each). – Wilmer Apr 26 '14 at 15:20
  • First, collect all the checked boxes. Then send this as an array or JSON to your backend. Send back *one* answer and do your stuff in the callback of your AJAX function. – Dirk McQuickly Apr 26 '14 at 15:23
  • @Nils you should read: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – A. Wolff Apr 26 '14 at 15:29
  • @A.Wolff i read the topic you send me and i will change the structure of my code. This asychronous/synchronous topic really is the worst thing about javascript :P – Nils Apr 26 '14 at 15:37

1 Answers1

-3

If you want to make the AJAX request synchronouse, pass async config as false.

$.ajax({
  dataType: "json",
  url: 'your URL',
  async: false,
  success: success
});

Documentation

Selvaraj M A
  • 3,006
  • 3
  • 27
  • 45
  • 1
    Users usually don't like freezed UI – A. Wolff Apr 26 '14 at 15:17
  • The question is about pausing the execution of the loop until the AJAX completes. 'async' does the job. – Selvaraj M A Apr 26 '14 at 15:19
  • @SelvarajMA it does do the job, but it uses the worst practice and may deadlock the browser (or the tab) if anything goes wrong (I down voted). – PhistucK Apr 26 '14 at 15:21
  • 1
    this was exactly what i needed. The "freezed" UI is something i can deal with. I will show a the loading indicator. Thank you very much! – Nils Apr 26 '14 at 15:25
  • @PhistucK SO is about answering the question. Not for preaching what you believe. And please look at Nil's comment above. Everyone has their own requirements. – Selvaraj M A Apr 26 '14 at 15:30
  • @Nils Can you please accept the answer if your question is resolved. – Selvaraj M A Apr 26 '14 at 15:30
  • @SelvarajMA But OP would have better to use promises interface returned by each request, or make only one request to retrieve all relevant data but certainly not setting to sync ajax requests. If for some reason he wants to use as many requests as input checked, he should look at `$.when` and apply it to an array of promises, something like this: http://stackoverflow.com/questions/4878887/how-do-you-work-with-an-array-of-jquery-deferreds – A. Wolff Apr 26 '14 at 15:35
  • @SelvarajMA I am not preaching what I believe. I am down voting a solution which may not work in the future, as browsers are heading towards deprecating the synchronous XMLHttpRequest. – PhistucK Apr 26 '14 at 15:35