当我在javascript中循环一个数组时,我会在每个项目后暂停3秒。它成功完成了这项工作,但它冻结了网页,直到数组完成。
https://forums.asp.net/t/2024891.aspx?WebImage+helper+multiple+image+upload
我尝试过setTimeout(),setInterval(0,delay(),2个不同的自定义睡眠函数和一个while循环。没有一个工作。
答案 0 :(得分:0)
使用它。在javascript中,当你执行一个需要时间x的while循环时,整个页面冻结了那个时间x。所以使用while循环是没有选择的。但是你可以像这样使用函数setTimeout。这将每10秒运行一次printNextElement函数(在我的示例中)。
在console.log处,做你的逻辑。并将10000更改为您的时间。
const ar = ['Hello', 'World'];
let index = 0;
const printNextElement = () => {
console.log(ar[index]);
index += 1;
if(ar.length === index) {
return;
}
window.setTimeout(printNextElement, 10000);
};
printNextElement();