Question

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.
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top