jQuery - 如果在代码末尾

时间:2016-05-16 17:04:57

标签: javascript jquery arrays json

使用此模式的数组temp1

[{"group":"Test","keywords":["Hund","Katze"]}]

我能够使用以下函数发出API请求以获取temp1.keywords的每个元素的同义词:

consultSynonyms(temp1)

    function consultSynonyms(x) {
        // create an empty array where all the responses are gping to be pushed
        // format has to be: [{x: xvalue, y: yvalue, z:zvalue...}]. This array
        // will be passed to data table plugin
        var jsonSynonyms = [];
        $.each(x, function(i, value) {
            // get name of group (f.i. Test)
            var superGroup = value.group;
            $.each(x[i].keywords, function(i, value) {
                // get term (f.i. Hund) and iterate all the terms
                var term = value;
                $.ajax({
                    type: "GET",
                    dataType: "jsonp",
                    url: "https://www.openthesaurus.de/synonyme/search?q=" + term + "&format=application/json&supersynsets=true",
                    success: function(data) {
                        $.each(data.synsets, function(key, value) {
                            // get category (f.i. "Biology") from response
                            var category = this.categories[0];
                            $.each(this.terms, function(key, value) {
                                // synonym is every term delievered by the API Call
                                // level is also delievered (f.i.: "formal", "informal"...)
                                var synonym = this.term;
                                var level = this.level;
                                jsonSynonyms.push({
                                    group: superGroup,
                                    keyword: term,
                                    category: category,
                                    term: synonym,
                                    level: level
                                });
                            });
                        });
                    }
                });
            });
        });
        console.log(jsonSynonyms);
    } 

但是,最后一个console.log没有提供预期的输出,只有一个简单的[]。我确信ajax代码是正确的,因为如果我在function(data)内移动console.log,我会得到预期的输出:

[{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Hund"},{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Vierbeiner"}...]

据我所知,我的函数末尾的console.log在最后评估,因此我不明白为什么我得到空白数组,而在中间评估它该功能提供正确的输出。是否有这种行为的暗示?

1 个答案:

答案 0 :(得分:1)

您使用AJAX请求,该请求开始同时执行其中的代码。一旦你去异步, 你永远不会回去

你知道,在 AJAX调用回来之后,将不会填充jsonSynonyms。这可能是任何时间,可能是100-200ms。但是,在进行AJAX调用后,您的console.log(jsonSynonyms)代码会立即执行 。所以jsonSynonyms无法填充。

AJAX调用返回后,您只能使用已填充的jsonSynonyms 。这意味着你应该在实际的AJAX回调中使用它。

function consultSynonyms(x) {
        // create an empty array where all the responses are gping to be pushed
        // format has to be: [{x: xvalue, y: yvalue, z:zvalue...}]. This array
        // will be passed to data table plugin
        var jsonSynonyms = [];
        $.each(x, function(i, value) {
            // get name of group (f.i. Test)
            var superGroup = value.group;
            $.each(x[i].keywords, function(i, value) {
                // get term (f.i. Hund) and iterate all the terms
                var term = value;
                $.ajax({
                    type: "GET",
                    dataType: "jsonp",
                    url: "https://www.openthesaurus.de/synonyme/search?q=" + term + "&format=application/json&supersynsets=true",
                    success: function(data) {
                        $.each(data.synsets, function(key, value) {
                            // get category (f.i. "Biology") from response
                            var category = this.categories[0];
                            $.each(this.terms, function(key, value) {
                                // synonym is every term delievered by the API Call
                                // level is also delievered (f.i.: "formal", "informal"...)
                                var synonym = this.term;
                                var level = this.level;
                                jsonSynonyms.push({
                                    group: superGroup,
                                    keyword: term,
                                    category: category,
                                    term: synonym,
                                    level: level
                                });
                            });
                        });

                        console.log(jsonSynonyms); // look at me now!
                    }
                });
            });
        });

    }