Hi,
On Wed, Dec 16, 2009 at 11:04 AM, jez9999 <[email protected]> wrote:
> It rather surprised me that one of the things I had to turn to a
> plugin for was a timer. It seems like the kind of thing that would
> frequently be useful to have, abstracted to a nice jQuery interface,
> rather than using directly the rather ugly setTimeout function in
> Javascript. Why not implement a timer interface in the jQuery core?
>
Well, there is *some* timer support in jQuery:
jQuery.animate(..)
for repetitive tasks, which also greatly simplifies the
attribute-interpolation.
Note that you can supply a step-callback:
jQuery.animate({},{step:function() {...}})
animate solves the chaining problem, too:
$.animate(...task1...).animate(...task2...);
Though, the building-blocks for chaining are also exposed in the API as
$.queue() and $.dequeue()
In my experience, the problem one want to solve with timers is either so
simple, that a
setInterval() / setTimeout()
already solves most of my problem, or it takes quite some code to make it
work.
In theses cases a timer framework (thats more than just a wrapper) would
have to provide A LOT of hooks, to cover all the use-cases --
or concentrate on a particular sort of tasks -- which is what $.animate()
does.
You could also then take this one step further and use 1ms one-off
> timers to implement multithreaded programming through jQuery, too. I
> think this should work - a Javascript engine has to have the ability
> to do multithreading if several timers can callback over time whilst
> other JS code may be executing, right? So we could have a timer and
> multithreads abstracted out nicely in the jQuery core. Both would be
> nice.
>
To my knowledge, the current javascript engines behave(!) "single-threaded",
i.e. a timer, event, etc. is only fired when it is not currently executing
javascript code (from that window/tab/page). Specifically, it waits for the
execution-context to finish before doing even a setTimeout(fn,0). Because of
this most of the multi-threading problems like concurrent access,
dead-locks... currently don't happen, and all the code with events and
timers "just works". To introduce true multi-threading would either required
mutexes, etc. or a synchronised data exchange (as done with
javascript-workers).
The only thing you could do is schedule one Task after the other. This is
what chaining and $.queue() already does.
At least this is my understanding of things -- if anyone know better, please
correct me...
Tobias
--
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en.