Skip to main content

Bug Tracker

Side navigation

#8489 closed enhancement (wontfix)

Opened March 10, 2011 10:05AM UTC

Closed March 30, 2011 07:08PM UTC

Last modified March 14, 2012 06:26PM UTC

Add a 'complete' parameter to .delay() for using callbacks. That was it's super simple to delay custom, non-jQuery functions.

Reported by: trusktr Owned by:
Priority: undecided Milestone: 1.next
Component: queue Version: 1.5.1
Keywords: Cc:
Blocked by: Blocking:
Description

For example, it'd be super nifty to do this:

$(this).delay(5000, function() {
    non_jquery_function();
});

The above would have the effect of executing non_jquery_function() after waiting 5 seconds when .delay() is added to the object's queue.

This would be super convenient!

Is there already a way to do this?

Attachments (0)
Change History (6)

Changed March 10, 2011 10:49AM UTC by trusktr comment:1

_comment0: How about \ \ {{{ \ $(this).delay(5000).queue( function(){ \ non_jquery_function(); \ $(this).dequeue(); \ }); \ }}} \ \ ? \ \ Is that the way to do it? If so, I'm not able to achieve my result still. :/1299755388123982
_comment1: I guess \ \ {{{ \ $(this).delay(5000).queue( function(next){ \ non_jquery_function(); \ next(); \ }); \ }}} \ \ ? \ \ Is the way to do it. However, it'd still be convenient to just use a callback within .delay().1299755483949106

I guess

$(this).delay(5000).queue( function(next){
    non_jquery_function();
    next();
});

?

Is the way to do it. However, it'd still be convenient to just use a callback within .delay(). It'd save you about 18 characters of typing. Besides, don't we want jQuery to be as easy to use as possible?

Changed March 10, 2011 11:01AM UTC by trusktr comment:2

_comment0: Actually, yeah, using .queue() is working. But it'd still be nice to just use delay with a callback function.1299755276815218
_comment1: Actually, yeah, using .queue() is working. But it'd still be nice to just use delay() with a callback function.1299755296912286

Changed March 10, 2011 04:50PM UTC by ajpiano comment:3

component: unfiledqueue

As it says on the docs for .delay()

"The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases."

I can't see why we'd add this parameter - I understand why one might think it convenient, but it's really just a super thin layer around setTimeout that obscures users from actually writing basic JavaScript. On the other hand, there might be a valid use case here, so I'm going to throw this into the feature proposals for 1.6 so we can get more opinions on this and we can evaluate based on whatever consensus is generated there.

Changed March 10, 2011 06:21PM UTC by scottgonzalez comment:4

I think the use case provided is a bit silly, but I think there are valid use cases and I would've expected this to work. I could see users wanting to do:

$( el )
    .animate( ..., fn1 )
    .delay( duration, fn2 )
    .animate( ..., fn3 );

Changed March 10, 2011 09:11PM UTC by trusktr comment:5

_comment0: I agree scott.gonzales: my example was very simple for the sole purpose of showing how it'd work. Of course, in a more realistic situation, you'd be calling .delay() amidst a chain of multiple jQuery animations where in the middle of this chain you might need to call a function that has no duration time associated with it but wherein you would still need a padding of time around that said function. This would come in handy when you want to pad .css(), .remove(), .append(), or any other durationless methods/functions with time. \ \ I'm guessing that using .delay() incorporates javascript's native setTimeout() somewhere in the source code, right? \ \ I guess the feature I'm asking for would do nothing more than the exact same thing you are doing when you use .queue() and .dequeue() like in my example above. It would be identical to the functionality of jQuery animations that use a "complete" parameter to launch functions only ''after'' animations are complete. In the case of .delay(), the animation is an animation-less animation that acts on nothing, but it still has a duration. \ \ All in all, it'd be a simple way to type a few less characters, while maintaining the same exact functionality as animated methods that wait the ''duration'' of their respective animations to execute the next function. \ \ Taking into account the time it took to type this reply, and if this feature request (enhancement) gets approved, then it'd be cool to know my time spent on this would save thousands of developers a few seconds each time they need to queue custom functions with a time delay, which would happen most often when they need to compensate for previous animations where the duration of said animations are known in order to execute functions that don't normally queue themselves to the queue.1299791562217270

I agree scott.gonzales: my example was very simple for the sole purpose of showing how it'd work. Of course, in a more realistic situation, you'd be calling .delay() amidst a chain of multiple jQuery animations where in the middle of this chain you might need to call a function that has no duration time associated with it but wherein you would still need a padding of time around that said function. This would come in handy when you want to pad .css(), .remove(), .append(), or any other durationless methods/functions with time.

ajpiano, I'm guessing that using .delay() incorporates javascript's native setTimeout() somewhere in the source code, right?

I guess the feature I'm asking for would do nothing more than the exact same thing you are doing when you use .queue() and .dequeue() like in my example above. It would be identical to the functionality of jQuery animations that use a "complete" parameter to launch functions only ''after'' animations are complete. In the case of .delay(), the animation is an animation-less animation that acts on nothing, but it still has a duration.

All in all, it'd be a simple way to type a few less characters, while maintaining the same exact functionality as animated methods that wait the ''duration'' of their respective animations to execute the next function.

Taking into account the time it took to type this reply, and if this feature request (enhancement) gets approved, then it'd be cool to know my time spent on this would save thousands of developers a few seconds each time they need to queue custom functions with a time delay, which would happen most often when they need to compensate for previous animations where the duration of said animations are known in order to execute functions that don't normally queue themselves to the queue.

Changed March 30, 2011 07:08PM UTC by ajpiano comment:6

resolution: → wontfix
status: newclosed

We looked at this proposal for 1.6, but the consensus was against landing this. We appreciate the time you took to put this together, but it introduces complexity and obscures around the fact that sometimes, using native JavaScript methods (setTimeout) or a plugin (like doTimeout is a better solution than us adding functionality directly to jQuery core.

(FYI, delay does use setTimeout internally.)