【发布时间】:2011-11-30 07:55:11
【问题描述】:
我想要做的是在两个同时通过 ajax 加载的图像完成时执行一些操作。为此,我创建了一个自定义 Deferred,以便在图像加载完成时解决。
<div id="i"></div>
$(document).ready(function() {
$('#i').hide();
var imgLoad = loadImgs();
$.when(imgLoad).then(function() {
$('#i').show('slow');
});
});
function loadImgs() {
var dfd = $.Deferred();
var matrix = $.getJSON("https://graph.facebook.com/thematrixmovie");
var pulp = $.getJSON("https://graph.facebook.com/pulpfiction");
$.when(matrix, pulp).then(function(m, p) {
mImg = '<img src=' + m[0].picture + '>';
pImg = '<img src=' + p[0].picture + '>';
$('#i').show().append(mImg + pImg);
dfd.resolve;
});
return dfd.promise();
}
你可以试试这个on JSFiddle。
我使用了 Eric Hynds post,包括 working example 作为参考,但仍然无法使用。有什么想法吗?
【问题讨论】:
标签: javascript jquery jquery-deferred