Using Deferreds

when you try to do multiple things at a time but can't be sure which one is going to get done first ,  the logic required can get a little hairy,requiring a number of state variables

or a bunch of nested callbacks, luckily, as of jQuery  1.5

all jQuery Ajax methods return an object known as a Deferred that

and when used correctly,

it can greatly simplify your callback code .

$.when($.getJSON('level1.json'),$.getJSON('enemies.json'),$.getJSON('player.json'))

.then(function(level,enemies,player){

}).fail(function(){

});

posted @ 2012-10-22 08:25  顺武  阅读(113)  评论(0编辑  收藏  举报