1

Im sorry i know that there are many questions regards this question but i couldn't apply it's answer on my code, maybe because im still new to js.

Here is my code, that code simply supposed to loop on product_id array but when i fire that function i see thatajax requests for all array elements got sent at once, what im trying to do is to start next loop after finishing the current loop.

function apm_update_all_products_ajax() {
  $product_ids = wc_get_products( array( 'return' => 'ids', 'limit' => -1 ) );
  ?>
  <button onclick="apm_update_all_products()">Update All Products</button>
      <script type="text/javascript" >
          function apm_update_all_products() {
          var product_ids = <?php echo json_encode($product_ids); ?>;
          for (var product_id of product_ids) {
                        $.ajax({
                            type: "POST",
                            url: ajaxurl,
                            dataType: "JSON",
                            data: {
                                    action: 'apm_update_single_product',
                                    postId: product_id
                            },
                            success: function(lookup_data) {

                            },
                            error: function(jqXHR, textStatus, errorThrown) {
                            }
                        })
          }
          };
    </script>
  <?php
  }

The reason i wanna do that is that this ajax send request to Amazon API which has a throtling limit so i cant send all requests at one time.

Islam Mohamed
  • 117
  • 2
  • 10
  • 1
    easiest is probably async&await – Chris Li Mar 25 '19 at 13:51
  • 1
    you should have a look at here - https://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-re But maybe you should tell us why you want to do this as it seems to be a not-so-good idea – Sindhara Mar 25 '19 at 13:52
  • Is there a specific reason why you want to do this? Right now, you're not actually doing anything in your callback, so I don't see why it matters if the previous request finishes before the next one starts. – Patrick Q Mar 25 '19 at 13:53
  • sorry i updated the question with the reason for doing that, it's because of API throtling limit that prevent me from sending several requests in a very short time – Islam Mohamed Mar 25 '19 at 13:57
  • 1
    If you're not actually doing anything with the response, another option would be to pass all of the ids in one request to your backend, and then have the backend code handle the throttling / multiple requests. – Patrick Q Mar 25 '19 at 14:01
  • @PatrickQ Unfortuinatly i cant do that because the API request for all ids will take to much time and can exceed the php timout, and if not then i think its not a good user experiance to not get response for several minutes thats why i decided to make it a request at a time and add a progress par or a precentage, but im still lost in finding answer to my question, i really read some answers but still unable to apply it to my code, the only one i was to apply is async:false but it freeze the UI – Islam Mohamed Mar 25 '19 at 14:20

0 Answers0