I often hear people say that anonymous function binding events are difficult to control, cannot be unbound, etc. I have always been surprised who said that they cannot be unbound.
The following is to implement automatic unbinding after the click event occurs twice.
Look at the code:
var dom=document.getElementById("test"),clickt=0;
dom.addEventListener("click",function(e){
clickt;
alert('you Touched me' clickt ', please touch it 2 times at most');
if(clickt>=2){
this.removeEventListener(e.type,arguments.callee,false);
}
});
Many extension libraries, such as jquery, can implement custom unbinding like this:
var t=0;
$(".a").bind("click",function(e){
t ;
alert('You touched me' t 'down. Touch 2 times at most');
if(t>=2){
$(this).unbind(e.type ,arguments.callee);
}
});
Test the others yourself.