koha345,
Такой итератор:
function iterator(array) {
var index = 0, cycle = 0;
return {
next: function () {
if (index >= array.length) index = 0, cycle++;
return {
cycle: cycle,
index: index,
value: array[index++]
}
}
}
}
var groups = ['a', 'b', 'c'];
var iterable = iterator(groups);
for (var i = 6; i--;) console.log(iterable.next()); // test
Как можно использовать:
function request(current) {
$.ajax({
complete: function (jqXHR, textStatus) {
if (current.cycle < 2) {
if (current.cycle == 0) {
console.log('task 0', current.value, current.index);
}
if (current.cycle == 1) {
console.log('task 1', current.value, current.index);
}
request(iterable.next());
} else {
console.log('no tasks');
}
}
});
}
request(iterable.next());
|