简体   繁体   English

等待所有功能和要求完成

[英]Waiting for all functions and requests to finish

Right now I've got some code that takes in an array of strings of the modules I wish to load 现在我有一些代码,它接收我希望加载的模块的字符串数组

script.js 的script.js

moduleLoader.load([
    'mainModule',
    'aboutUsModule',
    'featuresModule',
    'messageModule'
]);

The moduleLoader.load function does an $.each loop on the array and then proceeds to get the required information about each module. moduleLoader.load函数在数组上执行$.each循环,然后继续获取有关每个模块的必需信息。

moduleLoader ModuleLoader组件

var load = function (modules) {
    $.each(modules, function (index, module) {
        getConfig(module);
    });
};

getConfig getConfig

var getConfig = function (module) {
    $.get(module + '/config.json').done(function (config) {
        console.log(config);
        getData(config);
    }).fail(function () {
        console.error('Couldn\'t find a config file for module: ' + module);
        return 0;
    });
};

Then you can see in the getConfig callback that it proceeds to get data, which is also async, and then it has two more steps after that which is async. 然后你可以在getConfig回调中看到它继续获取数据,这也是异步的,然后它还有两个步骤,即异步。

So it's basically callback inside callback inside callback... etc.. 所以它基本上是在回调内部回调...等等。

Just that I divided it into functions so that it looks a bit nicer. 只是我将它划分为函数,以便它看起来更好一些。

Now I can get all the info, but it loads differently every time, so is it possible to know when all the ajax requests are done, and then do something? 现在我可以获取所有信息,但每次都加载不同,所以有可能知道所有ajax请求何时完成,然后做一些事情?

You could use the jQuery function $.when() that does exactly what you need: https://api.jquery.com/jquery.when/ 你可以使用jQuery函数$.when()来完成你所需要的: https//api.jquery.com/jquery.when/

To make your code work you can refactor a bit: 为了使你的代码工作,你可以重构一下:

var XHRs = [];

var load = function (modules) {
  $.each(modules, function (index, module) {
    XHRs.push(getConfig(module));
  });
};

$.when(XHRs).then(function() {
  // do something
});

And also your getConfig() function should return the $.get . 而且你的getConfig()函数也应该返回$.get This works because $.ajax in jQuery creates a Deferred object that actually lets you chain your functions or make them wait each other. 这是有效的,因为jQuery中的$ .ajax创建了一个Deferred对象,它实际上允许你链接你的函数或使它们彼此等待。

For future reference: https://api.jquery.com/jquery.deferred/ 供将来参考: https//api.jquery.com/jquery.deferred/

You can utilize promise chaining and group them all together and do something when they are all done (kind of like this): 您可以利用承诺链并将它们组合在一起并在完成后执行某些操作(有点像这样):

var load = function (modules) {
    var promises = modules.map(getConfig);

    // can use $.when or Promise.all here
    $.when.apply($, promises).then(function () {
         // do something when all done
    });
};

var getConfig = function (module) {
    // $.get returns a jqXHR which is "thennable"
    return $.get(module + '/config.json').then(function (config) {
        console.log(config);
        // if this also returns a promise then it will
        // wait till this one is done as well in the promise chain
        return getData(config); 
    }, function () {
        console.error('Couldn\'t find a config file for module: ' + module);
        return 0;
    });
};

If you have no other pending ajax requests, then use relevant global ajax event ajaxStop : 如果您没有其他待处理的ajax请求,则使用相关的全局ajax事件ajaxStop

$( document ).one('ajaxStop', function() {
  // all ajax requests has finished
});

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

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