> jQuery.fn.extend({ > every: function(interval,id,fn) { > return this.each(function() { > var self = this; > interval = jQuery.speed(interval); > if (fn == undefined) { > fn = id; > id = interval; > } > if (!this.timers) this.timers = {}; > if (!this.timers[id]) this.timers[id] = []; > this.timers[id].push(window.setInterval(function() { > fn.call(self); > },interval)); > }); > }, > doin: function(interval,id,fn) { > return this.each(function() { > var self = this, counter = 0; > interval = jQuery.speed(interval); > if (fn == undefined) { > fn = id; > id = interval; > } > if (!this.timers) this.timers = {}; > if (!this.timers[id]) this.timers[id] = []; > this.timers[id].push(window.setInterval(function() { > if (counter++ >= 1) return jQuery(self).stop(id); > fn.call(self); > },interval)); > }); > }, > stop: function(id) { > return this.each(function() { > if (!this.timers) return; > if (id == undefined) > jQuery.each(this.timers, function(i) { > jQuery.each(this,function(j) { > window.clearInterval(this); > }); > }); > else if (this.timers[id]) > jQuery.each(this.timers[id],function(i) { > window.clearInterval(this); > }); > }); > } > });
You can simplify that code quite a bit. every() and doin() are nearly identical, so they can both call a common implementation. Ditto for the two inner loops in stop(). (function( $ ) { function every(thing,interval,id,fn,counter) { return thing.each(function() { var self = this; interval = $.speed(interval); if (fn == undefined) { fn = id; id = interval; } if (!self.timers) self.timers = {}; if (!self.timers[id]) self.timers[id] = []; self.timers[id].push(window.setInterval(function() { if (counter != undefined && counter++ >= 1) return $(self).stop(id); fn.call(self); },interval)); }); } $.fn.extend({ every: function(interval,id,fn) { return every(this,interval,id,fn); }, doin: function(interval,id,fn) { return every(this,interval,id,fn,0); }, stop: function(id) { function clear(array) { if (array) $.each(array,function() { window.clearInterval(this); }); } return this.each(function() { if (!this.timers) return; if (id == undefined) $.each(this.timers, function() { clear(this); }); else clear(this.timers[id]); }); } }); })( jQuery ); Since I was moving the whole thing inside a function to get a local scope for the every() helper, that gave me a chance to rename jQuery as $. This is a matter of taste, of course, but I like being able to write plugin code the same way as ordinary jQuery code, using $. If you really wanted to get fancy, you could replace this bit of code: if (!self.timers) self.timers = {}; if (!self.timers[id]) self.timers[id] = []; self.timers[id].push(... with this: var timers = self.timers = self.timers || {}; ( timers[id] = timers[id] || [] ).push(... One last thing - in the inner function inside every(), I changed the uses of "this" to "self", because there's a "var self = this;" at the top of the function. I think it helps readability to only use "self" after a "var self = this;" instead of mixing "this" and "self". The code is untested, but should work the same as the original unless I goofed. :-) -Mike _______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/