首页 > 代码库 > jquery--延迟对象
jquery--延迟对象
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>???τ?</title> <script src="http://www.mamicode.com/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="http://www.mamicode.com/??"> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="http://www.mamicode.com/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>
jquery--延迟对象
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。