Is there a better way to send multiple AJAX requests in turn?

I have a page with several forms that I submit via AJAX POSTs one at a time. At first I tried using synchronous XHR requests, but this causes the browser to linger on the request time and breaks my DOM manipulation effects, which is unacceptable. So the template I used is basically like this:

var fcount = 0;    // incremented for each form to be submitted
function submit_form( num ) { 
    var fdata = { ... }; // data from form # num
    $.ajax( { async:    true,
              url:      '/index.cgi',
              data:     fdata,
              type:     'POST',
              success:  function() { 
                  if ( num < fcount ) { 
                      submit_form( ++num );
                  }
              }
           } );
}

$( '#submit_form_btn' ).click( function() { submit_form( 1 ) } );

Recursion amazes me as a slightly ugly solution to what is essentially an iterative problem. Is there a cleaner or more elegant way this could be handled?

+3
source share
4 answers

? , . AJAX.

UPDATE

, , jQuery Message Queuing, AJAX . , .

+10

(ajax-), , .

+1

, - script POST.

0

, , , , .

But the suspicious part here uses several calls, you spend a lot of time sending several calls. Insert all the data into one call and flip it if it becomes preferable in any way.

0
source

Source: https://habr.com/ru/post/1740598/


All Articles