1.引入 Deferred 对象的概念
1 var wait = function(dtd){ 2 3 var tasks = function(){ 4 5 alert("执行完毕!"); 6 7 dtd.resolve(); // 改变Deferred对象的执行状态 8 9 }; 10 11 setTimeout(tasks,5000); 12 13 return dtd.promise(); 14 15 }; 16 var wait1 = function(dtd){ 17 18 var tasks = function(){ 19 20 alert("执行完毕1!"); 21 22 dtd.resolve(); // 改变Deferred对象的执行状态 23 24 }; 25 26 setTimeout(tasks,1000); 27 28 return dtd.promise(); 29 30 }; 31 32 $.Deferred(wait) 33 34 .done(function(){ alert("哈哈,成功了!"); }) 35 36 .fail(function(){ alert("出错啦!"); }); 37 38 $.Deferred(wait1) 39 40 .done(function(){ alert("哈哈,成功了!1"); }) 41 42 .fail(function(){ alert("出错啦!"); });