这样做:
let withEachImageDo = (files) => {
files.forEach(function(imagePath, index) {
setTimeout(() => {
generateImageThumnail(imagePath);
}, index * 1000);
})
}
将在每个文件后减慢循环 1 秒。我如何在每 100 的倍数后等待 1 秒?
答案 0 :(得分:4)
您将有更好的时间将 withEachImageDo
转换为 async
函数,并从我们的工具包中获取一个可靠的旧异步延迟函数。
async function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
let withEachImageDo = async (files) => {
for (let index = 0; index < files.length; index++) {
const imagePath = files[index];
generateImageThumnail(imagePath); // This should probably be async too?
if (index && index % 100 == 0) {
await delay(1000);
}
}
};