$ .when,apply()和$ .done()如何在此函数中进行交互? [英] How are $.when, apply(), and $.done() interacting in this function?
问题描述
前段时间,一位SO用户为我编写了这个函数,返回一个可变数量的jQuery $。get()
个请求。第一部分非常简单,但是有人会如何解释 $。when()
, apply()
和 $。done()
正在进行交互。我一般不了解,但非常具体地说他们如何访问 $。get()
返回的延迟对象。
A while ago, a SO user wrote this function for me to return a variable number of jQuery $.get()
requests. The first part is pretty straightforward, but will someone elplain how $.when()
, apply()
, and $.done()
are interacting. I don't understand in general, but very specifically how they they are accessing the deferred object that $.get()
returns.
function getHTML(qty_of_gets) {
var dfdArr = [],
i = 1;
while (i <= qty_of_gets) {
dfdArr.push($.get("get_test_" + i + ".php"));
i++;
}
$.when.apply(null, dfdArr).done(function () {
for (var i = 0; i < arguments.length; i++) {
$("#content").append(arguments[i][0]);
}
});
}
推荐答案
$ .when
返回一个新的承诺,所以 $。when()。done()
只需调用 .done
在 $。返回时的承诺
。
$.when
returns a new promise, so $.when().done()
just calls .done
on the promise returned by $.when
.
.apply
让你调用一个函数使用数组中的参数而不是单独传递它们。所以
.apply
lets you call a function with arguments in an array instead of passing them individually. So
$.when.apply(null, dfdArr)
(几乎*)相当于
$.when(dfdArr[0], dfdArr[1], dfdArr[2], ...);
dfdArr
中的每个元素都是承诺。
Each of the elements in dfdArr
is a promise.
*除了这个
的价值功能。
这篇关于$ .when,apply()和$ .done()如何在此函数中进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!