On Nov 4, 5:54 pm, Pom <[EMAIL PROTECTED]> wrote:
> I wanted to check this on each iteration of the timer. In a perfect
> world the function fired by the timer would check if the element it
> was about to alter is still in the dom. If not, it should clear the
> timer associated with the element.
>
> Having only one timer would kinda make the whole code as a plugin
> useless. I wanted to be able to create multiple instances of the
> plugin on the same page which require one timer for each element.
> Guess I need to make a KYD. :(

Not really. All you need is a timer that triggers 30 times a second
(or whatever is your desired frame rate) and does the job for each of
the elements activated by the plugin. Instead of clearing intervals
for each element, you instead dequeue/de-register/remove it from the
list that the interval is checking.

Like this (sketch):

oneInterval = {
     init : function(){
         oneInterval.elements = $('nada');
         this.int = setInterval(oneInterval.animate,300);
     },
     stop : function() {
         clearInterval(this.int);
     },
     add : function(el) {
         this.elements = this.elements.add(el);
     },
     remove : function(el) {
         this.elements = this.elements.not(el);
     },
     animate : function() {
         oneInterval.elements.animate({'marginLeft':'+=10px'});
     }
};

<div id="one"></div>
<div id="two"></div>

oneInterval.init();
oneInterval.add('div');

then a few seconds later: oneInterval.remove('#one')

You could do the check only every 30 frames or so, saving you from
checking for every call that would certainly affect performance. That
is also possible with separate timers, you would add it inside each
one's interval function.

>
> "And you can't check for the element's
> existance anywhere within the plugin code because all event handlers
> have been removed with the element itself."
>
> I don't really understand this, or it isn't at all what I experience
> from testing. I have a console.log in the function fired by the timer
> created by the plugin. It outputs $(this).length, which would be 1
> since the plugin code attaches itself to each object supplied thru the
> jQuery object. If assign my rotate-plugin to for example $('#rotate')
> and then delete that same element, $(this).lenght will still output 1.
> If I however console.log($('#rotate').lenght) it would respond to the
> changes in the DOM.

I mean it's useless to put a check inside of 'onmousemove' for example
because that event will not be fired anymore, as it's owner element
has been removed from the DOM.

Reply via email to