Multiple Concurrent Ajax Calls

by john_s

HTML

<button id="testBtn" type="button" value="test">Test</button>

JavaScript

var CALL_DATA = [
    { html: 'test1', delay: 3 },
    { html: 'test2', delay: 1 }
];

$('#testBtn').click(function () {
    var calls = [];
    $.each(CALL_DATA, function (i, data) {
        calls.push($.ajax("/echo/html/", {
            type: 'post',
            dataType: 'html',
            data: {
                html: data.html,
                delay: data.delay
            }
        }));
    });
    $.when.apply($, calls).done(function () {
        alert('last call finished');
    });
});