首页 > 代码库 > jquery09--Callbacks : 回调对象
jquery09--Callbacks : 回调对象
<!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 : 回调对象
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。