Michael Geary wrote:
>
> 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().
Great suggestions Mike. I noticed that they were very similar but I was
too lazy to combine them but now you have challenged me, and I have
realized that just as with the immortals, there can be only one.
The code you modified was the original rewrite which didn't include the
additional passed arguments feature so my version of the rewrite is
slightly different but inspired by yours.
>
> 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 $.
It's definitely a matter of taste, as I tend to prefer to use the jQuery
in plug-in development seeing as the ultimate result is intended for
much generic uses than my page specfici code so it brings me into a
different more methodical mindset.
> 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".
That's probably because self was an afterthought. I always forget about
the intransitive nature of 'this' so my this closures are usually not
written until I realize I need them.
>
> The code is untested, but should work the same as the original unless I
> goofed. :-)
>
> -Mike
Well Actually it wouldn't have worked because I made a goof in my code!
Initially by setting interval to the result of jQuery.speed rather than
the duration property of the result. Noticed when testing the new code.
Here's the updated code (it'll probably be wrapped by my mail client):
jQuery.fn.extend({
every: function(interval,id,fn) {
return
jQuery.timer.add.apply(this,[false].concat(jQuery.map(arguments,"a")));
},
doin: function(interval,id,fn) {
return
jQuery.timer.add.apply(this,[true].concat(jQuery.map(arguments,"a")));
},
stop: function(id) {
return jQuery.timer.remove.apply(this,[id]);
}
});
jQuery.extend({
timer: {
add: function(oneOff,interval,id,fn) {
var args = jQuery.map(arguments,"a"), slice = 4;
return this.each(function() {
var self = this, counter = 0;
interval = jQuery.speed(interval)['duration'];
if (fn == undefined || id.constructor == Function) {
fn = id;
id = interval;
slice = 3;
}
if (!self.timers) self.timers = {};
if (!self.timers[id]) self.timers[id] = [];
self.timers[id].push(window.setInterval(function() {
if (oneOff && counter++ >= 1) return
jQuery(self).stop(id);
fn.apply(self,args.slice(slice));
},interval));
});
},
remove: function(id) {
return this.each(function() {
if (!this.timers) return;
jQuery.each(id == undefined ? this.timers :
[this.timers[id]], function(i) {
jQuery.each(this,function(j) {
window.clearInterval(this);
});
});
});
}
}
});
-blair
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/