[jQuery] A delay method?
[jQuery] A delay method?
For other ideas about time-based special effects, there may be some nuggets
in the Microsoft HTML+TIME stuff that they now seem to have abandoned:
http://msdn.microsoft.com/workshop/author/behaviors/reference/time2/htime_re
ference_entry.asp
There are a couple of samples here:
http://msdn.microsoft.com/library/en-us/dntime/html/timeanimation.asp
http://msdn.microsoft.com/library/en-us/dntime/html/htmltime.asp
The syntax is XML-based and mostly declarative, but there may be some
nuggets in there.
-----Original Message-----
From: discuss-bounces@jquery.com [mailto:discuss-bounces@jquery.com] On
Behalf Of Michael Geary
Sent: Friday, April 07, 2006 2:57 AM
To: 'jQuery Discussion.'
Subject: Re: [jQuery] A delay method?
So, Clarke was right! :-)
Here's the funny part: Some time ago when this same request came up, my
inner conversation went something like this: "No, you can't do that. Hmm...
But who says you always have to return the same 'this' object? What if you
returned a shadow object instead, that had all the same methods but just
stacked them up in a queue instead? Yeah, that would work... Naw... Is this
really a good idea? It requires every method to be duplicated, seems like a
lot of overhead when there's already a perfectly good way to do a time
delay. Forget about it!"
As you can see, I did such a good job forgetting about it that I went back
to thinking it was impossible!
(True story, honest! I'm not trying to cover up for my mistake. And I
certainly didn't think it through as far as you have.)
But, seeing it in action, maybe I'm not so convinced it's a bad idea after
all. Maybe it depends on how many of those shadow functions are required.
With the event plugin it would be over 200! It would be trivial to generate
those functions automatically in a loop, of course, but it seems that it
might be a bit time-consuming.
For anyone who still finds this boggling (and it does seem that way until
you see how simple it actually is), here's another way to think about it.
Forget about method chaining for the moment, and imagine that jQuery's $()
return object has a do() method that takes an array of method names and
their arguments, like this:
$('div').do(
[ 'hide', [ 'slow' ] ],
[ 'delay', [ '500' ] ],
[ 'show', [ 'slow' ] ]
);
Other than the fancy footwork for the delay method, it's obvious that a
simple loop could run through this array and call $.apply() on each of the
method-argument pairs. The fancy $Defer business essentially generates this
same array.
You could clean up the notation a bit by having do() process its arguments
in pairs:
$('div').do(
'hide', [ 'slow' ],
'delay', [ '500' ],
'show', [ 'slow' ]
);
That would mean you'd need to always provide the [] for a method that takes
no arguments, but that's not totally a bad thing, it makes it more
reminiscent of a function call:
$('div').do(
'hide', [],
'delay', [ '500' ],
'show', []
);
That's actually getting into the realm of something that wouldn't be too
onerous to code with. It's a lot uglier than the method chaining, but it
does the exact same thing and doesn't require the overhead of generating all
those shadow functions. After I run a test on a slow machine I'll know
whether I care about that overhead or not. :-)
In any case, looking at the code this way helps make something else clear
that might be confusing: With the deferred method chaining, all of the
arguments to all of the methods are evaluated immediately, before any of the
delays happen. With a conventional setTimeout() callback, the code inside
the callback function is not executed until the function is called. In the
simple examples here, that makes no difference, but it would in other cases.
-Mike