jq的回调对象是实现jq ajax和defferred的基础模块,通过add添加一系列的回调函数到队列中,并fire触发回调,当然你也可以通过remove删除队列中的回调函数。
1.基本过程,add,fire,remove,empty,has
var callbacks = $.Callbacks(); //获取回调队列
function fn1(value){
console.log("fn1 says : " + value);
}
function fn2(value){
console.log("fn2 says : " + value);
}
function fn3(value){
console.log("fn3 says : " + value);
}
callbacks.add(fn1); //添加回调函数
callbacks.add(fn2);
callbacks.add(fn3);
callbacks.fire("hell0"); //按照先进先出的规则依次调用回调
callbacks.remove(fn1);
console.log("fn1被删除了吗?" + callbacks.has(fn1));//判断队列中是否有某个回调函数
callbacks.fire("how are you");
callbacks.empty(); //直接清空队列回调函数
callbacks.fire("empty?");
2.disable和disabled
var callbacks = $.Callbacks(); //获取回调队列
function fn1(value){
console.log("fn1 says : " + value);
}
function fn2(value){
console.log("fn2 says : " + value);
}
callbacks.add(fn1);
callbacks.add(fn2);
callbacks.fire("hello"); //依次输入队列回调函数
callbacks.disable(); //不再调用队列函数,会清除队列里面所有的回调函数,还有使队列结构失效,不能再添加回调函数
console.log("队列是否失效:" + callbacks.disabled());
callbacks.add(fn1); //不能再添加回调函数
callbacks.fire("fire again"); //回调无效
3.lock和locked
var callbacks = $.Callbacks(); //获取回调队列
function fn1(value){
console.log("fn1 says : " + value);
}
function fn2(value){
console.log("fn2 says : " + value);
}
callbacks.add(fn1);
callbacks.add(fn2);
callbacks.fire("hello"); //依次输入队列回调函数
callbacks.lock(); //队列被数组
console.log("队列被锁住:" + callbacks.locked());
callbacks.add(fn1); //不能再添加回调函数
callbacks.fire("fire again"); //回调无效
lock和disable区别在于有没有标志位”memory”
1.若没有标志,lock和disable都将回调队列清空,并置为list = “”;
2.若有标志位,lock不会清空队列,只是把他锁住,这是fire是没有效的,除非你再add一个回调,然后就可以解锁了,disable会清空队列,并置list = “”;
var callbacks = $.Callbacks("memory"); //获取回调队列
function fn1(value){
console.log("fn1 says : " + value);
}
function fn2(value){
console.log("fn2 says : " + value);
}
callbacks.add(fn1);
callbacks.add(fn2);
callbacks.fire("hello"); //依次输入队列回调函数
callbacks.lock(); //队列被数组
console.log("队列被锁住:" + callbacks.locked());
//callbacks.add(fn1); //这里如果不用add,则后面的回调不会触发
callbacks.fire("fire again"); //回调无效
callbacks有三个标志位:
once: 保证只触发一次回调
var callbacks = $.Callbacks("once"); //获取回调队列
function fn1(value){
console.log("fn1 says : " + value);
}
callbacks.add(fn1);
callbacks.fire("hello"); //输出回调
callbacks.fire("hello again"); //不输出回调
memory: 第一次fire之后,后续add进来的函数自动利用上一次fire时候的参数执行
var callbacks = $.Callbacks("memory"); //获取回调队列
function fn1(value){
console.log("fn1 says : " + value);
}
function fn2(value){
console.log("fn2 says : " + value);
}
function fn3(value){
console.log("fn3 says : " + value);
}
callbacks.add(fn1);
callbacks.add(fn2);
callbacks.fire("hello"); //输出fn1和fn2回调
callbacks.add(fn3); //将fn3添加进去,然后利用上一次回调的参数"hello"来触发fn3一次回调
fn2("hello"),fn3("hello")
unique: 保证队列中的回调函数不会出现重复情况
stopOnFalse: 当队列中一个回调函数被调用返回false的时候,队列里的其他函数不会被调用
var callbacks = $.Callbacks("stopOnFalse"); //获取回调队列
function fn1(value){
console.log("fn1 says : " + value);
}
function fn2(value){
console.log("fn2 says : " + value);
return false ;
}
function fn3(value){
console.log("fn3 says : " + value);
}
callbacks.add(fn1);
callbacks.add(fn2); //这里调用会返回false,所以fn3不会被回调到
callbacks.add(fn3);
callbacks.fire("hello"); //只输出fn1,fn2
标志位可以重叠使用,用空格间隔开,deferred对象的三种状态的实现就标志位组合结果
jQuery.Callbacks( “once memory” ) - > “resolved” ,
jQuery.Callbacks( “once memory” ) - > “rejected” ,
jQuery.Callbacks( “memory” ) - > “progress”