-2

I have an array of checked items as code below:

$('input[name="numb_item"]:checked').each(function() {
var numb=this.value;
 $.ajax({
      dataType : 'json',
      data: {'numb_item': numb},
      type: 'POST',
      url:'ajxReportGenerator.php',
      success: function(data){
         $('#main_container').append(data);
      },
 });
});

My problem is that I wana pass first item and receive it's report then pass the secound one to the ajxReportGenerator.php but it will pass them once as separated parameters and it makes timeout error because in ajxReportGenerator.php my process will take long time and it's important to me to pass first item and receive it's report and then go to secound item...

Baya
  • 270
  • 1
  • 2
  • 8
  • 1
    you can't. .each executes synchronously. – Kevin B Aug 12 '14 at 17:49
  • 1
    [`async: false`](http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax) should do it, but I can't really recommend that. Instead, you should reconsider how you process the `data` you get in the `success` callback. – Blazemonger Aug 12 '14 at 17:51
  • Is it just that you want the results to be appended in a certain order? – Blazemonger Aug 12 '14 at 17:54
  • [look this answer.think this help](http://stackoverflow.com/a/4785886/1599937) – Aleksei Bulgak Aug 12 '14 at 18:04
  • Why you don't recommend async: false ? My function in php file is connecting to a low-speed webservice and it will give me timeout and the only think is that I have to proccess them one by one @Blazemonger – Baya Aug 12 '14 at 18:35
  • 1
    [This question and answer](http://stackoverflow.com/a/14220323/901048) covers that issue pretty thoroughly. Frankly, you should be trying to speed up that slow web service instead. – Blazemonger Aug 12 '14 at 19:24
  • You're right but the webservice is not mine and I have to get a long with it :(. Thanks for your responses. – Baya Aug 13 '14 at 06:17

1 Answers1

1

how about this loop like a tree:

<script>
var values = [];
$('input[name="numb_item"]:checked').each(function() {
    values.push(this.value);
});



loop = function(value,count,total,values){
    var numb = value;
     $.ajax({
          dataType : 'json',
          data: {'numb_item': numb},
          type: 'POST',
          url:'ajxReportGenerator.php',
          success: function(data){
             $('#main_container').append(data);
             count = count++;
             if(count < total){
                loop(values[count],count,total,values);
             }

          },
     });
}

count = 0;
total= values.length;
loop(values[count],count,total,values);

</script>
miglio
  • 1,978
  • 7
  • 20