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(){

});

你可能感兴趣的:(deferred)