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;
function submit_form( num ) {
var fdata = { ... };
$.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?
source
share