简体   繁体   English

如何等待我的AJAX请求完成?

[英]How can I wait until my AJAX request is complete?

So, I have an init function that runs when the DOM is fully loaded: 因此,我有一个init函数在DOM完全加载时运行:

function init() 
{
    TerrainObj.load_terrain();
    TerrainObj.generate_display_layer(PlayerObj);
    GameObj.update();
    ScreenObj.draw_screen(TerrainObj);
    ScreenObj.update_screen(TerrainObj);
    mainloop();
}

The very first method, TerrainObj.load_terrain() , makes an AJAX requestL: 第一个方法TerrainObj.load_terrain()发出AJAX requestL:

load_terrain: function()
{
    var settings = {
        type: "GET",
        url: "data/map.txt",
        aysnc: false
    };

    $.ajax(settings).done(function(result) 
    {
        this.terrain = result.split("\n");
        for (var i = 0; i < this.terrain.length; i++)
        {
            this.terrain[i] = this.terrain[i].split("")
        }
        console.log(this.terrain);
    });
}

The problem is that, in our init() function, most of the methods that follow TerrainObj.load_terrain() require the presence of the TerrainObj.terrain array , but they are called before the AJAX request can complete and that variable is populated. 问题在于,在我们的init()函数中,遵循TerrainObj.load_terrain()大多数方法都需要TerrainObj.terrain array的存在,但是在AJAX请求可以完成并填充该变量之前会调用它们。 I put a little message out when each function runs to check the order, and I get this: 当每个函数运行以检查顺序时,我都会提示一些信息,得到的是:

Ran TerrainObj.load_terrain()
Ran ScreenObj.draw_screen()
Uncaught TypeError: Cannot read property '0' of undefined 
[Array[55], Array[55], ... ]

So as you can probably see, ScreenObj.draw_screen , which needs TerrainObj.terrain, has run before the request can complete, and there's a TypeError when it tries to reference our object. 因此,您可能已经看到,需要TerrainObj.terrain的ScreenObj.draw_screen已在请求完成之前运行,并且在尝试引用我们的对象时发生TypeError It's not ready yet! 还没准备好! The very last message is the console.log() from the load_terrain method, indicating that the .done function runs afterwards. 最后一条消息是来自load_terrain方法的console.log() ,指示.done函数此后运行。

What can I do to wait until the AJAX request is complete before continuing on with my program? 在继续执行我的程序之前,我该怎么做直到AJAX请求完成?

Javascript is an event driven language. Javascript是一种事件驱动的语言。 You do not have a mainloop, but you have events that trigger what is happening. 您没有主循环,但是您有触发事件的事件。

Here, the completion of your ajax call triggers the subsquent setting up of the terrain. 在这里,ajax调用的完成触发了随后的地形设置。 Thus separate the function in two : 因此将函数分为两个部分:

init: function() 
{
    TerrainObj.load_terrain();
},
init_terrain: function()
{
    TerrainObj.generate_display_layer(PlayerObj);
    GameObj.update();
    ScreenObj.draw_screen(TerrainObj);
    ScreenObj.update_screen(TerrainObj);
    mainloop();
},
load_terrain: function()
{
    var settings = {
        type: "GET",
        url: "data/map.txt",
        aysnc: false
    };

    $.ajax(settings).done(function(result) 
    {
        this.terrain = result.split("\n");
        for (var i = 0; i < this.terrain.length; i++)
        {
            this.terrain[i] = this.terrain[i].split("")
        }
        this.init_terrain();
    });
}

Check the comment section in the below block of code 检查下面的代码块中的注释部分

    $.ajax(settings).done(function(result) 
    {
        this.terrain = result.split("\n");
        for (var i = 0; i < this.terrain.length; i++)
        {
            this.terrain[i] = this.terrain[i].split("")
        }
        /*******************************************/
        //Place a new function call here, to the function that contains the calls to your 
        //functions, or other logic, that require the array to be populated.
        /*******************************************/
    });

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

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