私はこのようなループを得ました:
for ( var current in all )
{
//load the item
prepare.load( all[current].resource , function( result ) {
doSomethingWithResult(result);
});
}
function AllItemsLoaded()
{
}
私の目標は、すべてのアイテムがロードされ、コールバック内のコードが実行された後に AllItemsLoaded() を実行することです。 .
Jquery Deferred/pipe を試してみましたが、コードは次のようになりました。
var chain = new $.Deferred().resolve();
for ( var current in all )
{
chain = chain.pipe(function(res){
prepare.load( all[current].resource , function( result ) {
doSomethingWithResult(result);
});
});
//if I do a return here, the pipe will continue without getting the result,
so I need to continue the pipe after load's callback and
doSomethingWithResult is executed
}
chain.done(AllItemsLoaded);