jquery--延迟对象
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ϞҪ͢τյ</title> <script src="jquery-2.0.3.js"></script> <script> jQuery.extend({ Deferred : function(){}, when : function(){} }); $.Deferred(); -> $.Callbacks(); $.when(); ғԙהгˇܹԚܘַߪעքì(3043 , 3183) Deferred : ғԙהг : הӬҽքͳһڜm ------------------------------------------------------------------------- var cb = $.Callbacks(); setTimeout(function(){ alert(111); cb.fire(); },1000); cb.add(function(){ alert(222); }); //Јկ111 ՙկ222 Ӭҽ ------------------------------------------------------------------------ var dfd = $.Deferred(); setTimeout(function(){ alert(111); dfd.resolve();//`̆Ԛfire },1000); dfd.done(function(){//`̆Ԛadd alert(222); }); //Јկ111 ՙկ222 Ӭҽ ------------------------------------------------------- setTimeout(function(){ alert(111); },1000); alert(222);//Јկ111 ՙկ222 Ӭҽ ------------------------------------------------------- var dfd = $.Deferred(); setTimeout(function(){ alert(111); dfd.notify();//`̆Ԛfire },1000); dfd.progress(function(){//`̆Ԛadd alert(222); }); //Јկ111 ՙկ222 Ӭҽ $.ajax({ url : 'xxx.php', success : function(){ alert('ԉ٦'); }, error : function(){ alert('ʧќ'); } }); //$.ajaxŚ׃ғԙ٦Ŝ $.ajax('xxx.php').done(function(){ alert('ԉ٦'); }).fail(function(){ alert('ʧќ'); }); ------------------------------------------------------------- var dfd = $.Deferred(); setTimeout(function(){ //alert(111); //dfd.resolve(); dfd.reject();//`̆Ԛfire },1000); dfd.done(function(){//`̆Ԛadd alert('ԉ٦'); }).fail(function(){//`̆Ԛadd alert('ʧќ'); }); //Јկ111 ՙկ222 Ӭҽ ---------------------------------------------------------- var dfd = $.Deferred(); setInterval(function(){ alert(111); dfd.resolve();//ԉ٦הӦdone dfd.reject();//ʧќהӦfail dfd.notify()//אהӦprogressìѐאܡԐ͵ì̹ӔҪlѸԥע },1000); dfd.done(function(){ alert('ԉ٦'); }).fail(function(){ alert('ʧќ'); }).progress(function(){ alert('א'); }); ----------------------------------------------------------------------- $(function(){ var cb = $.Callbacks('memory'); cb.add(function(){ alert(1); }); cb.fire(); $('input').click(function(){ cb.add(function(){//ԐmemoryҢȒfireӑޭִѐڽһՎ,bִܴѐfire(), alert(2); }); }); ---------------------------------------------------- var dfd = $.Deferred(); setTimeout(function(){ alert(111); dfd.resolve(); },1000); dfd.done(function(){//ֈresolve()ִѐկԶaaa, alert('aaa'); }); $('input').click(function(){//֣ܷдťկԶbbb,{ԃքߍˇcallbackքmemory dfd.done(function(){ alert('bbb'); }); }); }); --------------------------------------------------------------- done fail progress resolve reject notify ------------------------------------------------------------------------ function aaa(){ var dfd = $.Deferred(); alert( dfd.state() ); setTimeout(function(){ dfd.resolve(); alert( dfd.state() ); },1000); return dfd.promise(); } var newDfd = aaa(); newDfd.done(function(){ alert('ԉ٦'); }).fail(function(){ alert('ʧќ'); }); newDfd.reject(); ----------------------------------------------------------------------- alert(i^1);//0^1=1 1^1=0 var dfd = $.Deferred(); setTimeout(function(){ //dfd.resolve(); dfd.reject('hi'); },1000); dfd.always(function(){ alert('hello'); }); dfd.then(function(){//ԉ٦ܘַ alert(1); },function(){//ʧќܘַ alert(arguments[0]); },function(){//ѐאܘַ alert(3); }); ---------------------------------------------------------------------- var dfd = $.Deferred(); setTimeout(function(){ dfd.resolve('hi'); },1000); var newDfd = dfd.pipe(function(){ return arguments[0] + 'Įζ'; }); newDfd.done(function(){ alert( arguments[0] ); }); </script> </head> <body> <input type="button" value="֣ܷ"> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="jquery-2.0.3.js"></script> <script> var dfd = $.Deferred(); dfd.done();//完成触发成功的方法 未完成触发fail方法。 $.when().done();//when返回延迟对象,when可以对多个延迟对象做操作。 -------------------------------------------------------------------- function aaa(){ var dfd = $.Deferred(); dfd.resolve();//完成 dfd.reject(); return dfd; } function bbb(){ var dfd = $.Deferred(); //dfd.resolve(); dfd.reject();// return dfd; } aaa().done(function(){ alert('成功'); }); $.when( aaa() , bbb() ).done(function(){//aaa,bbb延迟对象都完成执行done,都失败走fail //aaa,bbb要返回~延迟对象 alert('成功'); }).fail(function(){ alert('失败'); }); $.when( ).done(function(){ alert('成功'); }); /*when的源码 return $.Deferred(); arguments[0] arguments[1] arguments[2] arguments[3] done() 计数器 :4*/ function aaa(){ var dfd = $.Deferred(); dfd.resolve(); return dfd; } function bbb(){ var dfd = $.Deferred(); dfd.reject(); //return dfd; } $.when( 123 , 456 ).done(function(){ alert(arguments[0]); alert(arguments[1]); alert('成功'); }).fail(function(){ alert('失败'); }); $.when( aaa() , 111 , bbb() , 222 ).done(function(){ alert('成功'); }).fail(function(){ alert('失败'); }); </script> </head> <body> </body> </html>