This is my sample code:
function out(msg)
{
$('#output').append(msg + '<br>');
}
var myDeferred = [];
$.each([8, 3, 4, 6, 9, 15, 7, 1], function (index, time)
{
myDeferred.push($.Deferred(function(dfd)
{
setTimeout(function ()
{
out(time);
dfd.resolve();
}, time * 1000);
}).promise());
});
$.when.apply($, myDeferred).then(function ()
{
out('all is done');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>
This output "1 3 4 6 7 8 9 15 all is done", all callback are called at the same time.
But I want "8 3 4 6 9 15 7 1 all is done", all callback are called one after the other.
Someone can help me ?
thanks in advance