Mootools Implementation for EveryTime
by ariehg
HTML
<a href='#' id='b'>click</a>
JavaScript
;(function(){
Function.implement({
everyTime : function(delay,times){
var $this = this, counter = 0,
handle = setInterval(function(){
$this();
if (counter++ == times) clearInterval(handle);
},delay);
this.__handle = handle;
return handle;
}
, stopTime : function(){
var handle = (this.retrieve)
? this.retrieve('everyTime:handle')
: this.__handle;
if (handle) clearInterval(handle);
return this;
}
});
Element.implement({
everyTime : function(func,delay,times){
var $this = this, count = 0, handle;
function timer(){
func();
if (count++ == times){
clearInterval(handle);
$this.eliminate('everyTime:handle');
}
}
handle = setInterval(timer,delay);
this.store('everyTime:handle',handle);
}
, stopTime : function(){
var handle = this.retrieve('everyTime:handle');
if (handle){
this.eliminate('everyTime:handle');
clearInterval(handle);
}
}
});
}).apply(this,[]);
var a = function a(){
console.log('a');
};
a.everyTime(200,5);
$('b').addEvent('click',function(){
this.everyTime(a,500,3);
});