Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的匿名回调无法正常工作_Javascript_Callback_Getjson_Asynccallback - Fatal编程技术网

JavaScript中的匿名回调无法正常工作

JavaScript中的匿名回调无法正常工作,javascript,callback,getjson,asynccallback,Javascript,Callback,Getjson,Asynccallback,我正在尝试使用JavaScript的匿名回调来运行一个函数,直到另一个函数完成。在第一个函数getDistrictions()中运行$getJSON调用,该调用需要在运行getRounds()函数之前完成。这是因为在通过另一个$getJSON返回相关轮之前,需要知道规程 在代码的底部,您可以看到返回的console.log命令的顺序。我可以看出问题在于getRounds()函数在getDistrictions()完全完成之前运行。我怎样才能解决这个问题 var my_Upcomings = []

我正在尝试使用JavaScript的匿名回调来运行一个函数,直到另一个函数完成。在第一个函数getDistrictions()中运行$getJSON调用,该调用需要在运行getRounds()函数之前完成。这是因为在通过另一个$getJSON返回相关轮之前,需要知道规程

在代码的底部,您可以看到返回的console.log命令的顺序。我可以看出问题在于getRounds()函数在getDistrictions()完全完成之前运行。我怎样才能解决这个问题

var my_Upcomings = []
var roundsCreated = {}
var my_RoundsList = []

getDisciplines("none", function() {
 console.log("check in getRounds will start")
 getRounds();
});

function getRounds(){
    console.log("start getRounds")
    console.log("my input Upcomings array:", my_Upcomings)
    for (var i = 0; i < my_Upcomings.length; i++) {
        console.log("check in get Rounds for loop")
        my_UpcomingIdString = my_Upcomings[i].poolId.toString()
            my_roundsUrl = startUrl + tournamentUrl + "/rounds/" + my_UpcomingIdString
                $.getJSON(my_roundsUrl, //output: {"1":"Round 1","2":"Round 2"}
                    function (json) {
                        my_CurrentRoundsCreated = json;
                        my_roundCount = Object.keys(my_CurrentRoundsCreated).length
                        my_Upcomings.roundsCreated = my_roundCount;
                        my_RoundsList.push(my_Upcomings.roundsCreated)
                        console.log(my_RoundsList)
                })
    }
}

function getDisciplines(param, callback){
    console.log("Now in get Disciplines")
    $.getJSON(listReadyMatchesUrl, function getUpcomingMatches (json) {
        my_Upcomings = json
        console.log("my upcoming matches", my_Upcomings)
        my_Upcomings.roundsCreated = {
                    "roundsCreated": "0"
                }
    })
 callback()
} 

//console.log:
  //Now in get Disciplines
  //check in now rounds will start
  //start getRounds
  //my input Upcomings array: Array[0]length: 0__proto__: Array[0]  
  //my upcoming matches [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
var my_Upcomings=[]
var roundsCreated={}
var my_RoundsList=[]
GetDictions(“无”,函数(){
log(“签入getRounds将启动”)
getRounds();
});
函数getRounds(){
log(“启动getRounds”)
log(“我的输入上传数组:”,我的上传)
对于(var i=0;i
回调的位置错误。这是正确的方法:

function getDisciplines(param, callback){
    console.log("Now in get Disciplines")
    $.getJSON(listReadyMatchesUrl, function getUpcomingMatches (json) {
        my_Upcomings = json
        console.log("my upcoming matches", my_Upcomings)
        my_Upcomings.roundsCreated = {
                    "roundsCreated": "0"
                }
     callback(); <-- Notice this line INSIDE the $.getJSON callback
    });
}
函数getDistrictions(参数,回调){
log(“现在在获取规程中”)
$.getJSON(listReadyMatchesUrl,函数getUpcomingMatches(json){
my_Upcomings=json
log(“我即将到来的比赛”,我的未来)
my_Upcomings.roundsCreated={
已创建roundsCreated:“0”
}

callback();调用success callback中的
callback
,callback属于
$。getJSON
位于
gettrictions
中。太好了,这就成功了,谢谢!