Once again, I have an 11th hour code change. I had forgotten about 
Jörn's suggestion of allowing additional passed arguments. I'm not 
entirely sure what kind of implementation or usage was expected but I 
added my own version. Any argument provided to the .doin()/.every() 
function beyond the action function will be passed to the action 
function as an argument. Hopefully that's what he was talking about. 
Code Below:

jQuery.fn.extend({
    every: function(interval,id,fn) {
        var args = jQuery.map(arguments,"a"), slice = 3;
        return this.each(function() {
            var self = this;
            interval = jQuery.speed(interval);
            if (fn == undefined || id.constructor == Function) {
                fn = id;
                id = interval;
                slice = 2;
            }
            if (!this.timers) this.timers = {};
            if (!this.timers[id]) this.timers[id] = [];
            this.timers[id].push(window.setInterval(function() {
                fn.apply(self,args.slice(slice));
            },interval));
        });
    },
    doin: function(interval,id,fn) {
        var args = jQuery.map(arguments,"a"), slice = 3;
        return this.each(function() {
            var self = this, counter = 0;
            interval = jQuery.speed(interval);
            if (fn == undefined || id.constructor == Function) {
                fn = id;
                id = interval;
                slice = 2;
            }
            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.apply(self,args.slice(slice));
            },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);
                });
        });
    }
});

Blair Mitchelmore wrote:
> Good suggestions John as always. .in() can't be done because 'in' is a 
> JS keyword and I wouldn't want to have to call it like $()['in']() all 
> the time. So I figured two JS keywords smashed together would work and 
> chose .doin() as the function name; if you have any better ideas (maybe 
> .timeout() and .interval()) let me know.
>
> So this can handle multiple different one-off delays and interval-ed 
> events. These can then be canceled based on their reference label or 
> their interval if you didn't provide a label (not both).
>
> Example:
> $('p.display')
>     .doin(500,function() { window.alert("a"); })
>     .doin(1000,function() { window.alert("b"); })
>     .doin(500,function() { window.alert("c"); })
>     .doin(1000,'other',function() { window.alert("d"); })
>     .doin(500,'other',function() { window.alert("e"); });
>
> // Alerts: 'a','b','c'
> $('p.display').stop('other');
>
> // Alerts: 'b','d','e'
> $('p.display').stop(500);
>
> // Alerts: 'a','c','d','e'
> $('p.display').stop(1000);
>
> // Alerts Nothing
> $('p.display').stop();
>
> I'll probably host this on my server if there's any interest. Any Questions?
>
> -blair


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to