Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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 异步jQuery getJSON调用后访问变量_Javascript_Jquery_Asynchronous_Getjson - Fatal编程技术网

Javascript 异步jQuery getJSON调用后访问变量

Javascript 异步jQuery getJSON调用后访问变量,javascript,jquery,asynchronous,getjson,Javascript,Jquery,Asynchronous,Getjson,我不熟悉异步Javascript调用,所以我很难弄清楚在某个对象的数据被getJSON调用修改后如何访问它 我有一个空数组(单词)的对象,还有一个main方法和一个子方法。main方法获取一些JSON,并基于JSON数据多次调用subMethod。subMethod访问不同的JSON文件,并使用该文件填充对象的空数组(单词) 当我创建对象时,我调用main方法。我想获取对象的数组,但只有在它被JSON调用填充之后 我知道它必须与延迟方法或其他东西有关,但我在调用中不断得到一个空白数组。我该怎么做

我不熟悉异步Javascript调用,所以我很难弄清楚在某个对象的数据被getJSON调用修改后如何访问它

我有一个空数组(单词)的对象,还有一个main方法和一个子方法。main方法获取一些JSON,并基于JSON数据多次调用subMethod。subMethod访问不同的JSON文件,并使用该文件填充对象的空数组(单词)

当我创建对象时,我调用main方法。我想获取对象的数组,但只有在它被JSON调用填充之后

我知道它必须与延迟方法或其他东西有关,但我在调用中不断得到一个空白数组。我该怎么做?谢谢

对象的代码:

    function Something(numTerms){
        var self=this;
        this.words=[];
        this.wordsNeeded=numTerms;
        this.mainMethod = function(){
            $.getJSON("external JSON url", function(data) {
                var setIds=self.getDifferentSets(data, self.wordsNeeded).ids;
                for(var i=0; i<setIds.length; i++){
                    self.subMethod(setIds[i]);
                }   

            });
        }
     this.getDifferentSets= function(data, num){
        var obj= {};
        obj.ids=[];
        obj.wordsFound=0;
        var currSet=0;
        while(obj.wordsFound<num){
                obj.ids.push(data.sets[currSet].id);
                obj.wordsFound+=data.sets[currSet].term_count;
                currSet++;
        }
        return obj;
    }
        this.subMethod= function(id){
            $.getJSON("some other external JSON url", function(data) {
                var numberTerms= data.terms.length;
                var terms=[];
                if(self.wordsNeeded<numberTerms) numberTerms=self.wordsNeeded;

                for(var i=0;i<numberTerms;i++){
                    var word= new Object();
                    word.term=data.terms[i].term;
                    word.definition=data.terms[i].definition;
                    terms.push(word);
                }
                for(var i=0;i<terms.length;i++){
                    var word= {"term":terms[i].term, "definition":terms[i].definition};
                    self.words.push(word); //Changing the object's variable
                }
                self.wordsNeeded-=numberTerms;
            });
        }
    }

您能否尝试在主函数中返回此

function Something(numTerms){
    var self=this;
    //Blah blah blah

    return this;
}

在fetch buttin中调用some.mainMethod()时,任务是异步的,这意味着后面的var单词不会等待调用完成,而some.words将为空。为了正确地获取捕获数据,您需要在收到数据后调用另一个函数,该函数位于GetJSON附加的函数中,但我如何访问在我的$(“#fetch_按钮”)中收到数据后调用的另一个函数的结果。单击block?调用该函数后无法直接获取该函数的结果,因为它是异步的,所以需要等待:)。这就是为什么你必须声明一个新函数。示例:用户单击buton,出现一个进度条,您调用getJson函数,一旦完成,您将获取数据,隐藏进度条,并最终访问您刚刚获取的数据。好的,我尝试了。然后,如果我使用console.log(some),它确实表明某些内容的字数是正确的。但是,我尝试console.log(一些.words),它仍然返回未定义的。
function Something(numTerms){
    var self=this;
    //Blah blah blah

    return this;
}