gpt4 book ai didi

javascript - 如何等到数组被填满(异步)

转载 作者:数据小太阳 更新时间:2023-10-29 04:59:27 24 4
gpt4 key购买 nike

我有一个异步填充的数组,包含 28 个项目。我想等到数组填满所有项目。

function checkIfFinished(){
return(Results.length >= 28);
}

var isfinished = false;
while(isfinished){
if(checkIfFinished()){
returnResults();
isfinished = true;
}
else
//Wait 100ms
}

好吧,但是在 Javascript 中没有等待函数!我用 setTimeout 试过了,但我不知道如何插入它......我只是得到太多递归和东西的错误 :D

谢谢!

最佳答案

尝试:

var timeout = setInterval(function() {
if(checkIfFinished()) {
clearInterval(timeout);
isFinished = true;
}
}, 100);

这将每 100 毫秒调用您的检查函数,直到 checkIfFinished() 返回 true 给您。

关于javascript - 如何等到数组被填满(异步),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8267857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com