views:

100

answers:

2

I am having problems using multiple queues in jQuery. Consider the following example:

$('#example').click(function() {
  $(this).delay(1000, 'fx2').queue('fx2', function() {
    alert('here');
  });
});

The alert never fires. Why?

A: 

Try the following jsFiddle example. It seems to work and do what you want.

Alex Winston
Except that is using the `fx` queue not the `fx2` queue as the OP wants. -- Also it's still worth including the actual code in the SO link as opposed to pushing the actual answer to jsFiddle/jsbin
gnarf
Alex, you miss the point of my question completely. I don't want to use the standard fx queue (as your working example does).
kingjeffrey
+4  A: 

It seems when calling delay (or any other animations for that matter) on a custom queue you need to also set that queue in motion first using .dequeue()

When .dequeue() is called, the next function on the queue is removed from the queue, and then executed. This function should in turn (directly or indirectly) cause .dequeue() to be called, so that the sequence can continue.

$('#example').click(function() {
  $(this).delay(1000, 'fx2').queue('fx2', function(next) {
    alert('here');
    // start the next anim in the queue...
    next();
  }).dequeue('fx2');
});

jsbin preview

Note that the callback on queue receives a function as its first argument. This is the function you want to call whenever your "animation" is finished so that the next item in the queue can execute.

The jQuery code handles 'auto-starting' the fx queue in the $.fn.queue() function:

if ( type === "fx" && queue[0] !== "inprogress" ) {
  jQuery.dequeue( this, type );
}
gnarf
An easier way to do this is `function(n) { alert('here'); n(); }` :)
Nick Craver
+1 - Although the code seems to work fine without `$(this).dequeue('fx2');` in the callback. Is it necessary? Seems like the chained `dequeue()` must be getting called?
patrick dw
+1 For the auto-start note, but I'd still use the next function provided as the argument to the callback you're queuing, much cleaner :)
Nick Craver
It works, but it wont continue further down the queue unless you add the `dequeue()` (or call the first argument to the queue callback function as Nick suggested)
gnarf
@Nick Craver - Updated, and agreed - much cleaner ;)
gnarf
@gnarf - Thanks for the reply. I don't quite grasp how queues work. Nothing a little quality tinker time won't cure. :)
patrick dw
Nick Craver and gnarf... you guys rock! That is exactly what I needed. +1s all around.
kingjeffrey