English 中文(简体)
是否有可能将单一参数通过至美元,其中含有两个递延物体?
原标题:Is it possible to pass a single parameter to $.when which contains two deferred objects?

我只想在 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.

是否有明智的方法这样做?

最佳回答

可将一系列推迟的物体通过$>。

$.when.apply($, myArray).done(...);
问题回答
var imgDefs = $("img").map(function(key, val) {
  var def = $.Deferred();
  $(val).load(def.resolve);
  return def;
}).toArray();

$.when.apply($, imgDefs).done(...);




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...