如何等待直到数组被填充(异步)

如何等待直到数组被填充(异步)

问题描述:

我有一个异步填充的数组,包含28个项目.我要等到数组充满所有项目为止.

I have an array which is filled asynchronous and contains 28 items. I want to wait until the array is filled with all items.

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

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

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

Well, but in Javascript there is no wait function! I tried it with setTimeout, but I don't know how to insert it... I just get errors with too much recursion and stuff :D

谢谢!

尝试:

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

这将每100毫秒调用一次您的检查功能,直到checkIfFinished()反馈给您为止.

This will call your check-function every 100 ms until checkIfFinished() gives true back to you.