Maybe you can use promises like this
// number of runs
var times = 3;
// number of items
var items = 6;
// makes a promise which will resolve when all items are made
let makeItem = () => {
return new Promise( (done) => {
for(var i = 0; i < items; i++){
console.log('Item #0' + i);
}
done();
});
}
// run asynchronoulsy and wait until makeItem is done to execute next iteration
let run = async () => {
for(var i = 0; i < times; i++){
console.log('Run #' + (i + 1));
await makeItem();
}
}
run();
Edit: Sorry your code uses jquery so I updated my answer using jquery as well:
// number of runs
var times = 3;
// number of items
var items = 6;
container = $("#container");
function makeItem(it, el) {
for(var x = 0; x < it; x++)
{
el.append("<li>item #0"+x+"</li>");
}
}
async function run(times){
let result;
for(var i = 0; i < times; i++){
container.append("<h2>Run #"+(i + 1)+"</h2>");
try{
result = await makeItem(items, container);
}catch(error){
console.log(error);
}
}
}
run(times);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…