简体   繁体   English

JavaScript范围问题和混乱

[英]Javascript scope problems and confusion

I know scope should be simple. 我知道范围应该很简单。 I can see that when going through the documentation on scoping, and when reading responses to the other people who've asked this here. 在浏览有关范围界定的文档时,以及在这里阅读对其他提出此要求的人员的答复时,我都可以看到。 That's why I'm confused. 这就是为什么我很困惑。 Here's the relevant chunk of code I'm working with: 这是我正在使用的相关代码块:

$.ajax({
            type: "GET",
            url: locationURL,
            success: function (result) {
                console.log(result);
                var output = "<table><thead><tr><th>ID</th><th>Subject</th></thead><tbody>";
                for (var i in result.messages) {
                    var id = result.messages[i].id;
                    var locationURL = "https://www.googleapis.com/gmail/v1/users/me/messages/" + id + "?access_token=" + token;
                    var subject = "test";
                    $.ajax({
                        type: "GET",
                        url: locationURL,
                        success: function (result) {
                            subject = result.payload.headers[0].name;
                            console.log(subject);
                        }
                    });
                    console.log(subject);
                    output += "<tr><td>" + id + "</td><td>" + subject + "</td></tr>";
                }
                output += "</tbody></table>";

                display.html(output);
                $("table").addClass("table");
            }
        });

As you can probably tell, I am working with the Gmail API. 如您所知,我正在使用Gmail API。 So what am I doing here? 那我在这里做什么? The first ajax call is getting the list of all message ids. 第一个ajax调用获取所有消息ID的列表。 Then, for each message id I need to request message details, hence the nested ajax calls. 然后,对于每个消息ID,我需要请求消息详细信息,因此需要嵌套的Ajax调用。

Now, the confusing part is this: In the outer ajax call's success function I define var subject = "test" . 现在,令人困惑的部分是:在外部ajax调用的成功函数中,我定义了var subject = "test" I intend to overwrite this with the subject line from each message. 我打算用每封邮件的主题行覆盖此内容。 Before I specifically look for the subject line in the payload of the message details, I just want to get some new string instead of test. 在我专门在消息详细信息的有效负载中查找主题行之前,我只想获取一些新字符串而不是进行测试。 In the success function on the nested ajax call I set subject = result.payload.headers[0].name; 在嵌套ajax调用的成功函数中,我设置subject = result.payload.headers[0].name; and the subsequent 'console.log(subject)' shows me that var subject has a new value. 随后的“ console.log(subject)”向我显示var subject具有新值。 However, back in the outer ajax, when I add the next id and subject to my table, the value of subject still tests. 然而,早在阿贾克斯外,当我下一次加idsubject ,以我的表,价值subject仍然测试。

I would appreciate any pointers, thanks! 我将不胜感激任何指示,谢谢!

"Ajax" stands for "Asynchronous JavaScript and XML", meaning it's asynchronous . “ Ajax”代表“异步JavaScript和XML”,这意味着它是异步的 So whenever you call $.ajax, that code runs at the same time as the code right after that ajax call. 因此,每当调用$ .ajax时,该代码就会该ajax调用后的代码同时运行。 This can be very useful, but also quite annoying in situations like yours where there's stuff outside the ajax call that depends on the data from the Ajax call. 这可能非常有用,但在像ajax调用之外的其他内容取决于Ajax调用中的数据的情况下,这也很烦人。 Typically this is what the success function is used for. 通常,这就是成功功能的用途。

If you want a quick and dirty fix, you could add async:false as a key:value pair in your ajax call, although this is heavily frowned upon because it ruins the asynchronous power of ajax. 如果您想进行快速而肮脏的修复,则可以在ajax调用中添加async:false作为key:value对,尽管由于它破坏了ajax的异步功能,所以对此一无所知。

See this answer for information on how to make asynchronous ajax calls inside for loops work. 有关如何在for循环中进行异步ajax调用的信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM