简体   繁体   English

javascript-执行功能的顺序

[英]javascript - Order of executing functions

I have some javascript functions being called on Document Ready: 我在“文档就绪”中调用了一些javascript函数:

fogFields();
getLoS();
getShips();    

startGame();
getNextMove();

However, it is as though getNextMove() is being called first, most likely as all it does is an ajax call and alerts the result. 但是,好像首先要调用getNextMove()一样,最有可能的是,它所做的只是ajax调用并警告结果。 All the other functions have more work, so, the first thing that happens on load is the getNextMove() alert, and in the background you can see that none of the other functions did their work. 所有其他函数的工作量都更大,因此,加载时发生的第一件事是getNextMove()警报,在后台您可以看到其他函数都没有发挥作用。 Until I click OK on the alert window, no results are shown. 在单击警报窗口上的“确定”之前,不会显示任何结果。 Can I make it so that until a function finishes, the next wont even start. 我能做到这一点,以便在功能完成之前,下一个甚至不会启动。 Some functions call their own extra functions before they finish, and that works in order, but I cant do that with the whole code... 一些函数在完成之前会调用它们自己的额外函数,这些函数可以按顺序工作,但是我无法用整个代码来做到这一点。

Given the code in your question, there is no way the call to getNextMove can be invoked before startGame has been exited, regardless of their contents. 鉴于你的问题的代码,就没有办法调用getNextMove之前可以被调用startGame已经退出,而不考虑其内容。

It may be true that a function that has been scheduled asynchronously (via timeout, AJAX callback etc.) within startGame completes at any time before or after the invocation of getNextMove , but this is a separate issue. 这可能是真的,已经异步地调度功能(通过超时,AJAX回调等) startGame之前或调用之后的任何时间完成getNextMove ,但这是另外一个问题。 To resolve that issue we need to know more about the contents of the functions. 为了解决该问题,我们需要更多地了解函数的内容。

If the other functions have an AJAX call in them, then these AJAX calls most certainly take a callback argument, which is a function that gets executes, when the AJAX call has finshed. 如果其他函数中有AJAX调用,则这些AJAX调用最有可能带有回调参数,该参数将在AJAX调用完成后被执行。 Now, if you want to execute your functions in a way, the one function starts when the AJAX call of the previous function finished, you can add an additional callback argument to your own functions, which will then be passed to the AJAX calls. 现在,如果要以某种方式执行函数,则在上一个函数的AJAX调用完成时,一个函数将启动,您可以在自己的函数中添加一个附加的回调参数,然后将其传递给AJAX调用。 This should illustrate what I mean: 这应该说明我的意思:

function logFields(callback) {
    ajaxCall(callback);
}

function getLoS(callback) {
    ajaxCall(callback);
}

function getShips(callback) {
    ajaxCall(callback);
}

function startGame(callback) {
    ajaxCall(callback);
}

function getNextMove() {
}

fogFields(function(){
    getLoS(function(){
        getShips(function(){
            startGame(function(){
                getNextMove();
            });
        });
    });
});

If all of your functions use a ajax call then just use promises. 如果您所有的函数都使用ajax调用,则只需使用promises。 Simply return it, for example: 只需将其返回,例如:

function fogFields(){
    return $.ajax();
};

and then just use .then: 然后只需使用.then:

fogFields().then(getLos());

more information about deffered object on jquery doc page if you use it. 如果使用了有关延迟对象的更多信息,请访问jquery doc页面 Or implementation in pure javascript you can find here and more theory here . 或纯JavaScript实现,你可以找到在这里多的理论在这里
or another option, which I will not recommend you is to set async param in $.ajax call to false . 或其他选项,我不建议您将$ .ajax调用中的async参数设置为false Again it's for case you use jQuery. 同样,这是您使用jQuery的情况。

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

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