tags:

views:

241

answers:

1

hi, what is wrong in this code? I'm trying to get this effect: fadeOut(500) and attr('class','myClass') delayed by 600 millisecs.. then delay(600) again, and fadeIn(500). The delays happen correctly but the attr() is not being delayed, it fires when #myDiv is still fading! :'(

$('#myDiv').fadeOut(500).delay(600).attr('class','myClass').delay(600).fadeIn(500);  

many thanks in advance for any suggestions! Luca

+4  A: 

The .delay() only affects the animation or fx queue (unless you specify a different queue specifically). Keep in mind that chaining and queuing are 2 distinctly different concepts, chaining continues the use of the same jquery set, but that's a different thing entirely than any event queues on elements in that set.

To have the .attr() call affected, you have to add it as a callback to that same queue using .queue(), like this:

$('#myDiv').fadeOut(500)
           .delay(600)
           .queue(function(next) { $(this).attr('class','myClass'); next(); })
           .delay(600)
           .fadeIn(500); 

Also note there are .addClass(), .removeClass() and .toggleClass() methods available that may make this a bit cleaner :)

Nick Craver
I think you should call `$(this).dequeue()` inside the function. From the docs: *Note that when adding a function with .queue(), we should ensure that .dequeue() is eventually called so that the next function in line executes.* Anyway +1 ;)
Felix Kling
@Felix - Woops I'm usually calling this as the last item in the queue, good catch :)
Nick Craver
Hi Nick,thanks a lot for your answer. It works as you say, however the following calls in queue don't work anymore.. :(EDIT saw only now Felix comment.. thanks a lot to both of you guys
luca
@luca - Make sure you're trying the latest answer...that's the thing Felix pointed out in comments, it should work now :)
Nick Craver