Javascript $.when.done回调不是';行不通

Javascript $.when.done回调不是';行不通,javascript,jquery,callback,.when,Javascript,Jquery,Callback,.when,以下代码有一个错误的语法错误。可能是因为我用了“for”之类的词 $.when( for (var i=0; i < 5; i++) { $.getScript( "'" + someArr[i].fileName + ".js'"); } $.Deferred(function( deferred ) { $( deferred.resolve ); }) ).done(function() { aler

以下代码有一个错误的语法错误。可能是因为我用了“for”之类的词

$.when(
    for (var i=0; i < 5; i++) {
        $.getScript( "'" + someArr[i].fileName + ".js'");
    }
    $.Deferred(function( deferred ) {
              $( deferred.resolve );
    })
).done(function() {
    alert("done");
});
$。什么时候(
对于(变量i=0;i<5;i++){
$.getScript(“+somarr[i].fileName+.js'”;
}
$.Deferred(函数(延迟){
$(deferred.resolve);
})
).done(函数(){
警惕(“完成”);
});
我试图调用几个脚本,然后当trey全部加载时,我想显示一个警报。

下面是一个带有更改的注释(但未经测试)解决方案

// When takes a promise (or list of promises), not a random string of javascript
$.when((function() {
    // First we need to define a self resolving promise to chain to
    var d = $.Deferred().resolve();

    for ( var i = 0; i < 5; i++ ) {

        // Trap the variable i as n by closing it in a function
        (function(n) {

            // Redefine the promise as chaining to itself
            d = d.then(function() {

                // You can *return* a promise inside a then to insert it into
                // the chain. $.getScript (and all ajax methods) return promises
                return $.getScript( someArr[n].fileName + '.js' );
            });

        // Pass in i (becomes n)
        }(i));
    }

    return d;

// self execute our function, which will return d (a promise) to when
}())).then(function() {

    // Note the use of then for this function. done is called even if the script errors.
    console.log( 'done' );
});
下面是一个经过评论(但未经测试)的解决方案

// When takes a promise (or list of promises), not a random string of javascript
$.when((function() {
    // First we need to define a self resolving promise to chain to
    var d = $.Deferred().resolve();

    for ( var i = 0; i < 5; i++ ) {

        // Trap the variable i as n by closing it in a function
        (function(n) {

            // Redefine the promise as chaining to itself
            d = d.then(function() {

                // You can *return* a promise inside a then to insert it into
                // the chain. $.getScript (and all ajax methods) return promises
                return $.getScript( someArr[n].fileName + '.js' );
            });

        // Pass in i (becomes n)
        }(i));
    }

    return d;

// self execute our function, which will return d (a promise) to when
}())).then(function() {

    // Note the use of then for this function. done is called even if the script errors.
    console.log( 'done' );
});

如果我理解正确,可以使用
$.map()
$.when.apply()
简洁地编写您想要的代码,如下所示:

// First scan someArr, calling $.getScript() and building 
// an array of up to 5 jqXHR promises.
var promises = $.map(someArr, function(obj, index) {
    return (index < 5) ? $.getScript(obj.fileName + ".js") : null;
});
// Now apply the promises to $.when()
$.when.apply(null, promises).done(function() {
    alert("done");
});

其中
jqXHR0
等是由五个
$.getScript()
调用返回的
jqXHR
对象。

如果我理解正确,可以使用
$.map()
$.when.apply()
简洁地编写您想要的代码,如下所示:

// First scan someArr, calling $.getScript() and building 
// an array of up to 5 jqXHR promises.
var promises = $.map(someArr, function(obj, index) {
    return (index < 5) ? $.getScript(obj.fileName + ".js") : null;
});
// Now apply the promises to $.when()
$.when.apply(null, promises).done(function() {
    alert("done");
});

其中,
jqXHR0
等是由五个
$.getScript()
调用返回的
jqXHR
对象。

而且我很抱歉使用了horiizing代码。我一点也不知道如何使用这个函数。这是一个比我更好的解决方案。执行
$.when.apply($
而不是
null
$.when()是否更安全
不会利用
这个
,所以只要它不产生错误,你就可以把你喜欢的东西放在那个位置。
null
和其他任何东西一样好。这是一个比我的更好的解决方案。当.apply($而不是
null
$)时,使用
$会更安全吗
不会利用
这个
的漏洞,所以只要不产生错误,你就可以把你喜欢的东西放在那个位置。
null
和其他任何东西一样好。