使用call和apply调用函数有什么区别?
var func = function() {
alert('hello!');
};
Run Code Online (Sandbox Code Playgroud)
func.apply(); VS func.call();
上述两种方法之间是否存在性能差异?当它最好使用call过apply,反之亦然?
我有一个三层深度的延迟ajax调用链,理想情况下,当最深层完成时,它们会一直向前推进(使我成为Inception ..."我们需要更深入!").
问题是我一次发送了许多ajax请求(可能是数百个),需要推迟直到所有这些请求完成.我不能依赖最后一次完成.
function updateAllNotes() {
return $.Deferred(function(dfd_uan) {
getcount = 0;
getreturn = 0;
for (i = 0; i <= index.data.length - 1; i++) {
getcount++;
$.when(getNote(index.data[i].key)).done(function() {
// getNote is another deferred
getreturn++
});
};
// need help here
// when getreturn == getcount, dfd_uan.resolve()
}).promise();
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一些简单的Javascript,它使用Trello API从我的帐户获取所有板/列表/卡并将它们添加到可排序的表中(使用Datatables jquery插件).
到目前为止,我设法编写了一个获取所有这些信息并将其写入页面的jsfiddle,但我无法弄清楚如何将所有这些信息存储到某种数据结构中,然后可以将其传递给datatable插件.
到目前为止,这是从Trello获取数据的小提琴:
var carddata = [];
Trello.members.get("me", function(member) {
$("#fullName").text(member.fullName);
var boardUrl = "";
boardUrl = "members/me/boards";
Trello.get(boardUrl, function(boards) {
$.each(boards, function(ix, board) {
Trello.get("/boards/" + board.id + "/lists", function(lists) {
$.each(lists, function(ix, list) {
Trello.get("lists/" + list.id + "/cards", function(cards) {
$.each(cards, function(ix, card) {
console.log("boardname: " + board.name + "; list name: " + list.name + "; card name: " + card.name);
carddata.push(
"boardname: " + board.name +
"; list name: " …Run Code Online (Sandbox Code Playgroud) 我想写一个执行十几个异步ajax请求的函数,等待所有的完成,然后返回聚合信息.喜欢:
function countHealthy(urls) {
var healthy = 0;
for (var i = 0; i < urls.length; ++i) {
$.ajax({url: urls[i], async: true, id: i})
.done(function (data) { ++healthy; });
}
// Wait for all ajax to finish.
return healthy;
}
Run Code Online (Sandbox Code Playgroud)
PS:++健康线程安全吗?
关于承诺的另一个问题.我有这种情况:
Service.prototype.parse = function (data) {
var deferred = $.Deferred();
var arr = [];
for (var i = 0; i < data.length; i++) {
var details = new Details();
$.when(details).then(function (data) {
arr.push(data);
deferred.resolve(arr);
});
}
return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)
代码中的其他地方:
...
$.when(parse()).then(function (resp) {
//...
});
Run Code Online (Sandbox Code Playgroud)
承诺在某些时候得到解决,但最初resp的长度为1.
如何等待parse()解决所有问题并返回数组?
javascript ×3
jquery ×3
ajax ×1
asynchronous ×1
dynamic ×1
function ×1
loops ×1
performance ×1
promise ×1
trello ×1