Store a javascript interval in a jQuery element.data object? -


I am working on a jQuery plugin (a jQuery UI widget) where I need to store specific reference contexts For every individual example of the plugin on the page, if I just write something like this:

PreInterval = setInterval (function () (warning ("Hi!"), 100);

... The variable myInterval will actually be window.myInterval and it will be shared by all the examples on the page So when I try to set or clear an interval for an example, the interval for all instances will be set / cleared because they all refer to the same window variable.

By looking through the jQuery API, I found this method to be used to store arbitrary data associated with the matched elements and since each instance of the plug-in has the corresponding DOM element best! By using this method, I can store the data that is specific to the matching element, so I have structured my code in such a way (where is a reference to the element of L. Milan):

 El.data ("myInterval", setInterval (function () (warning ("hi!");}, 100)); 

To some extent this works because different examples of the plugin no longer refer to the same variable (interval). But I have noticed that if I have 2-3 examples of the plugin and I have set an interval which is similar to each example, only the first example starts its interval but if the interval timer is small, then only Some elements start their gaps

I have not dig deep in the real values, but if I have three instances of plug-ins on one page and set to interval 5, then this way:

 El .data ("myInterval", setInterval (function () (warning ("hi!");}, 5)); 

... Only the first example will start. If I increase it by 20-30, then two instances will start and if I move it, say, 50 will be the beginning of all three instances. it's so weird?!

My question is:

What is the interval reference to accumulate (using data) is a good idea? If yes, then what could be the reason for the failed interval?

Please help me with this one - I'm stumped! If you want the original source code, see the plugin page (the link at the top of this post).

/ Thomas Kahn

return value of setInterval and setTimeout only reference to expiration There are intrinsically used integers, so storing an integer with .data is not a problem.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -