当函数中存在“ for循环”时,如何使回调正确执行?

时间:2018-10-16 16:26:02

标签: javascript callback asynccallback

由于我有一个“ for循环”和多个if / else场景,因此似乎从未触发过该回调。当我删除“ for循环”时,它可以正常工作。我在这里做错了什么?如何强制回调等待所有for循环和条件完成?谢谢!

var myArray = ["one","two","three","four","five","six","seven","eight","nine","ten"];

function myFunction(arg1,arg2,arg3,arg4,arg5,callbackFunction){

        if (arg1 == "arg1"){

            for (var i=0; i<=myArray.length; i++){

                    if(arg2 == "arg2"){

                        if(arg3 == "arg3"){    

                            if(arg4 == "arg4"){

                                 //some code

                            }else{ 
                                if(arg5 == "arg5"){
                                        //some code
                                    }else{
                                        //some code
                                    }
                                //some code
                            }
                        }else{ 
                           //some code
                        }
                    } 
                    else{
                        //some code
                    }
            }
        }else{
            //some code
        }  

        if (callbackFunction){
            callbackFunction();
        }
}

myFunction("arg1","arg2","arg3","arg4","arg5",function(){
    alert("done");                                            
});

1 个答案:

答案 0 :(得分:0)

由于上面的注释,我意识到,由于我正在与After Effects交互,因此该程序强制该函数返回某些内容,并且不允许触发回调。我只是在“ for”声明之后和所有条件之前添加了此代码:

if(i==myArray.length){break;}

就解决了。谢谢大家!

R