0

I have an array of multiple ajax calls inside a when then function and would like to display a preloder or a text before every call.

Let's say before every call I want to show: "processing task id 1..." and when finished then "processing task id 2..." and so on until the end that it's being cleared.

I tried to use beforeSend on the ajax call but always displays the first id which is 1. any ideas?

var ids = [1, 2, 3, 4, 5]; // task ids
var deferreds = [];
//
$.each(ids, function(i, id_task){
    deferreds.push(
        $.ajax({
            type: "POST",
            url: '/tasks/import',
            data: $.param({id_task: id_task}),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},

            // always displaying "processing task id 1..."
            beforeSend: function(){
                $('#iter_msgs').html("processing task id " + id_task + "...");
            }
            //

        })
            .success(function(response){
                // more stuff
            })
    );
});
//
$.when
    .apply($, deferreds)
    .done(function(a) {
        //
        $('#iter_msgs').html('')
    });

So far I want to achieve the equivalent of the following but instead using when, then:

//
var id_task = 1;
$('#iter_msgs').html("processing task id " + id_task + "...");

//
$.ajax({
    type: "POST",
    url: '/tasks/import',
    data: $.param({id_task: id_task}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
    .success(function(response) {
        $('#iter_msgs').html("");
    });
Uuid
  • 2,506
  • 5
  • 28
  • 37

0 Answers0