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

});

原文地址:https://www.cnblogs.com/yushunwu/p/2733286.html