【问题标题】:How to make following code execute once require() has completed? Require JSrequire() 完成后如何执行以下代码?需要 JS
【发布时间】:2014-09-07 04:19:58
【问题描述】:

我想知道如何确保方法doStuffWithModules(modules) 在方法require() 完成执行后执行。

例如:

var moduleNames = ['moduleOne', 'moduleTwo'], //An array of module names, which could be of any length
    modules = [];

require(moduleNames, function() {

    for(var i = 0; i < arguments.length; i++) {
        modules.push(arguments[i]);
    }
});

doStuffWithModules(modules);

解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: javascript asynchronous requirejs require


    【解决方案1】:

    只需将doStuffWithModules 移动到require 函数中:

    var moduleNames = ['moduleOne', 'moduleTwo'];
    //An array of module names, which could be of any length
    
    require(moduleNames, function() {
        var modules = []
        for(var i = 0; i < arguments.length; i++) {
            modules.push(arguments[i]);
        }
        doStuffWithModules(modules);
    });
    

    【讨论】:

    • 当然!有时我觉得我对最简单的解决方案视而不见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2014-10-27
    • 2015-10-27
    相关资源
    最近更新 更多