jQuery中的$.Callbacks回调
1 function fn1(){ 2 console.log(1); 3 } 4 function fn2(){ 5 console.log(2); 6 } 7 var callbacks = $.Callbacks(); 8 callbacks.add(fn1); 9 callbacks.add(fn2); 10 callbacks.fire();// 1 2
当回调函数在不同的作用域也可以添加在一个队列中。
1 function fn1(){ 2 console.log(1); 3 } 4 var callbacks = $.Callbacks(); 5 !function(){ 6 function fn2(){ 7 console.log(2); 8 } 9 callbacks.add(fn2); 10 }(); 11 callbacks.add(fn1); 12 callbacks.fire();// 2 1
1 function fn1(){ 2 console.log(1); 3 } 4 function fn2(){ 5 console.log(2); 6 } 7 function fn3(){ 8 console.log(3); 9 } 10 var callbacks = $.Callbacks(); 11 callbacks.add(fn1); 12 callbacks.add(fn2); 13 callbacks.add(fn3); 14 callbacks.fire(); // 1 2 3
1 callbacks.remove(fn1, fn2); 2 callbacks.fire(); // 3
1 console.log(callbacks.has()); // true 2 console.log(callbacks.has(fn1)); // true
1 var name = ‘window‘; 2 var person = {name: ‘hum‘}; 3 function fn1(){ 4 console.log(this.name); 5 } 6 var callbacks = $.Callbacks(); 7 callbacks.add(fn1); 8 callbacks.fireWith(window); // window 9 callbacks.fireWith(person); // hum
1 var name = ‘window‘; 2 var person = {name: ‘hum‘}; 3 function fn1(value){ 4 console.log(value + this.name); 5 } 6 function fn2(value1, value2){ 7 console.log(value1+ value2 + this.name); 8 } 9 var callbacks = $.Callbacks(); 10 callbacks.add(fn1); 11 callbacks.add(fn2); 12 callbacks.fireWith(window, [‘window:‘, ‘age:‘]); // window:window window:age:window 13 callbacks.fireWith(person, [‘person:‘]); // person:hum person:undefinedhum
1 function fn1(){ 2 console.log(1); 3 } 4 function fn2(){ 5 console.log(2); 6 } 7 var callbacks = $.Callbacks(‘once‘); 8 callbacks.add(fn1); 9 callbacks.fire(); // 1 10 callbacks.add(fn2); 11 callbacks.fire(); // 不会触发
2、memory:记忆当次调用fire和fireWith,在遇到下次调用fire或fireWith之前,只要回调对象调用add方法都能执行,遇到下次调用时才会计算队列的remove、empty、add关系。
1 function fn1(){ 2 console.log(1); 3 } 4 function fn2(){ 5 console.log(2); 6 } 7 var callbacks = $.Callbacks(‘memory‘); 8 callbacks.add(fn1); 9 callbacks.fire(); // 1 2 10 callbacks.add(fn2); 11 12 callbacks.fire(); // 1 2 2 13 callbacks.empty(); 14 callbacks.add(fn2); 15 16 callbacks.fire(); // 2 1 17 callbacks.add(fn1);
3、unique:去从。
1 function fn1(){ 2 console.log(1); 3 } 4 function fn2(){ 5 console.log(2); 6 } 7 var callbacks = $.Callbacks(‘unique‘); 8 callbacks.add(fn1); 9 callbacks.add(fn2); 10 callbacks.add(fn2); 11 callbacks.fire(); // 1 2
4、stopOnFalse:当有一个对调函数返回false时,后面的回调都不会执行了。
1 function fn1(){ 2 console.log(1); 3 return false; 4 } 5 function fn2(){ 6 console.log(2); 7 } 8 var callbacks = $.Callbacks(‘stopOnFalse‘); 9 callbacks.add(fn1); 10 callbacks.add(fn2); 11 callbacks.fire(); // 1
这四种类型可以组合使用,用空格隔开。
1 function fn1(){ 2 console.log(1); 3 return false; 4 } 5 function fn2(){ 6 console.log(2); 7 } 8 var callbacks = $.Callbacks(‘once memory‘); 9 callbacks.fire(); 10 callbacks.add(fn1); // 1 2 11 callbacks.add(fn2); 12 callbacks.fire(); // 没有输出
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。