help please:
For example i have 20 Timeouts, created by window.setTimeout and i want that they run one after the other:
call timeout 1 => after finish call timeout 2 => after finish call timeout 3 and so on.
Generally there is an array of functions and the number of timeouts not fixed.
my function with deferred:
function someFunction(index) {
console.log("someFunction called, index = " + index);
var $deferred = $.Deferred();
window.setTimeout(function () {
$deferred.resolve();
}, 2000);
return $deferred.promise();
}
for loop:
var $deferred;
$(someArray).each(function (index) {
if (!$deferred) {
$deferred = someFunction(index);
} else {
$deferred.then(function () {
return someFunction(index);
});
}
});
all the others are run immediately without in the chain