嵌套的For循环超时打字稿

时间:2019-03-19 16:58:36

标签: javascript angular timeout nested-loops

我试图运行一个两级超时的两级嵌套循环,但是启动时并没有停止。我不知道失败在哪里,因为我只是按照一些教程进行操作,并最终得到以下解决方案:

checkDistances(){
    this.pageInfo.tracking = true;
        for(let indexOrigin = this.pageInfo.currentArea; indexOrigin < this.pageInfo.areas.length; indexOrigin++ ){
            for(let indexDestination = this.pageInfo.currentArea +1; indexDestination< this.pageInfo.areas.length; indexDestination++){
                this.checkDistance(indexOrigin, indexDestination);
            }
        }
}

 checkDistance(indexOrigin, indexDestination){
    setTimeout(()=>{
        this.pageInfo.origin        =   this.pageInfo.areas[indexOrigin];
        this.pageInfo.destination   =   this.pageInfo.areas[indexDestination];
    },2000*indexOrigin)

}

pageInfo.currentArea以0开头; 任何帮助将不胜感激。

0 个答案:

没有答案