How can I send ajax in a for loop by order? Here is my code:
function ajax_sent(i, btn){
jQuery.ajax({
url : ln_ajax_handle.ajax_url,
type : 'post',
data : {
action : 'ln_ajax_handle',
main_data : i
},
success : function(result){
jQuery('#ln-modal-bangchinh').append(result);
if(i === 5){
btn.button('reset');
jQuery('#ln-content-modal').modal('show');
}
}
});
}
jQuery(document).ready(function(){
jQuery('.ln-update').click(function(){
var btn = jQuery(this);
btn.button('loading');
for(var i = 1; i <= 5; ++i){
ajax_sent(i, btn);
}
});
});
And here is simple php test function:
function ln_ajax_handle(){
$data = $_POST['main_data'];
echo ln_alert_test($data);
wp_die();
}
function ln_alert_test($data){
ob_start();
?>
<div class="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Success!</strong> The loop run by <?php echo $data; ?> times;
</div>
<?php return ob_get_clean();
}
In php file, I use echo
to get simple test. But the results are numbers is not sort by order, like as image. . I want when i = 1
, and I get responsed from server, i = 2
will be execute. and next progress is same. Any way to do this?