<!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不谈,有memory又调用一次fire() -------------------------------------------------------------------------- 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>