jquery09--Callbacks : 回調對象
阿新 • • 發佈:2017-05-28
ucc val 對象 rem doc var out btn notify
<!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> //Callbacks管理回調。------------------------------------------------------- function aaa(){ alert(1); } function bbb(){ alert(2); } function ccc(){ alert(3); } var cb = $.Callbacks(); cb.add( aaa ); cb.add( bbb ); cb.add( ccc ); cb.fire(); //綁定事件,觀察者模式 document.addEventListener(‘click‘,function(){ alert(1); },false); document.addEventListener(‘click‘,function(){ alert(2); },false); document.addEventListener(‘click‘,function(){ alert(3); },false); --------------------------------------------------------------------------- function aaa(){ alert(1); } (function(){ function bbb(){//局部作用域 alert(2); } })(); aaa(); bbb(); ---------------------------------------------------------------------------var cb = $.Callbacks(); function aaa(){ alert(1); } cb.add(aaa); (function(){ function bbb(){ alert(2); } cb.add(bbb); })(); cb.fire();//1 2 ---------------------------------------------------------------------- once memory unique stopOnFalse add remove has empty disable disabled lock locked fireWith fire fired ------------------------------------------------------------------------- function aaa(){ alert(1); } function bbb(){ alert(2); } function ccc(){ alert(3); } var cb = $.Callbacks(); cb.add( aaa ); cb.add( bbb ); cb.add( ccc ); cb.remove( bbb ); cb.fire(); </script> </head> <body> </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> function aaa(){ alert(1); } function bbb(){ alert(2); } function ccc(){ alert(3); } ------------------------------------------------------------------------- var cb = $.Callbacks(‘once‘); cb.add( aaa ); cb.add( bbb ); cb.fire(); cb.fire();//觸發一次 ------------------------------------------------------------------------- var cb = $.Callbacks(‘memory‘); cb.add( aaa ); cb.fire(); cb.add( bbb );//彈出來,不寫memory不談 -------------------------------------------------------------------------- var cb = $.Callbacks(‘unique‘); cb.add( aaa ); cb.add( aaa ); cb.fire();//只執行一次 --------------------------------------------------------------------------- function aaa1(){ return false; alert(1); } var cb = $.Callbacks(‘stopOnFalse‘); cb.add( aaa1 ); cb.add( bbb ); cb.fire();//只談1不談2 ---------------------------------------------------------------------------- var cb = $.Callbacks(‘once memory‘);//組合形式 cb.add( aaa ); cb.fire();//2也彈出來 cb.add( bbb ); cb.has (bbb );//true cb.fire();//不執行 ------------------------------------------------------------ options : { once : true , memory : true } optionsCache : { ‘once memory‘ : { once : true , memory : true } } ---------------------------------------------------------- function aaa(n){ alert(‘aaa‘ + n); return false; } function bbb(n){ alert(‘bbb‘+n); } var cb = $.Callbacks(); cb.add(aaa,bbb); cb.fire(‘hello‘); cb.fire(‘hello‘); </script> </head> <body> </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 bBtn = true; function aaa(){ alert(1); if(bBtn){ cb.fire(); bBtn = false; } } function bbb(){ alert(2); } var cb = $.Callbacks(); cb.add( aaa ); cb.add( bbb ); cb.fire(); ---------------------------------------------------------------------- function aaa(){ alert(1); } function bbb(){ alert(2); } var cb = $.Callbacks(‘once memory‘); cb.add( aaa ); cb.fire(); cb.add( bbb ); cb.fire(); --------------------------------------------------------------------------- function aaa(){ alert(1); } function bbb(){ alert(2); } var cb = $.Callbacks(‘memory‘); cb.add( aaa ); cb.fire(); cb.lock(); cb.add( bbb ); cb.fire(); </script> </head> <body> </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> /*jQuery.extend({ Deferred : function(){}, when : function(){} }); $.Deferred(); -> $.Callbacks(); $.when();*/ /*var cb = $.Callbacks(); setTimeout(function(){ alert(111); cb.fire(); },1000); cb.add(function(){ alert(222); }); var dfd = $.Deferred(); setTimeout(function(){ alert(111); dfd.resolve(); },1000); dfd.done(function(){ alert(222); }); setTimeout(function(){ alert(111); },1000); alert(222);*/ /*var dfd = $.Deferred(); setTimeout(function(){ alert(111); dfd.notify(); },1000); dfd.progress(function(){ alert(222); });*/ /*$.ajax({ url : ‘xxx.php‘, success : function(){ alert(‘成功‘); }, error : function(){ alert(‘失敗‘); } });*/ //$.ajax(‘xxx.php‘).done(function(){ alert(‘成功‘); }).fail(function(){ alert(‘失敗‘); }); /*var dfd = $.Deferred(); setTimeout(function(){ //alert(111); //dfd.resolve(); dfd.reject(); },1000); dfd.done(function(){ alert(‘成功‘); }).fail(function(){ alert(‘失敗‘); });*/ /*var dfd = $.Deferred(); setInterval(function(){ alert(111); //dfd.resolve(); //dfd.reject(); dfd.notify(); },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(){ alert(2); }); });*/ /* var dfd = $.Deferred(); setTimeout(function(){ alert(111); dfd.resolve(); },1000); dfd.done(function(){ alert(‘aaa‘); }); $(‘input‘).click(function(){ 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); /*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>
jquery09--Callbacks : 回調對象