我只想在 j子的所有图像都装满时执行某些代码。 我可以采取以下行动:
var img1Load = $.Deferred();
$( #img1 ).load(img1Load.resolve);
var img2Load = $.Deferred();
$( #img2 ).load(img2Load.resolve);
$.when(img1Load, img2Load).done(function(){
console.log( both imgs loaded );
});
这是科索沃,但我不必为每一种形象确定一个单独的推迟目标。 如果有数百人,或者他们可能或不可能出现在某一页? 我愿一劳永逸地这样做。
$.when(allImagesHaveLoaded).done(function(){
console.log( all imgs loaded );
});
I have tried creating an allImagesHaveLoaded
function which iterates over the images, creating a new deferred object for each. The trouble is, if I return an array of these to be used as the parameter for a when, it doesn t work (because it s not a deferred object, it s an array of deferred object).
I fiddled around with $.Deferred.pipe
a bit as well (to try to create a chained deferred object to pass to when
), but that didn t appear to help.
是否有明智的方法这样做?