jQuery .delay() not delaying the .html() function

Derek Adair picture Derek Adair · Aug 13, 2010 · Viewed 19.2k times · Source

I'm trying to do a little javascript trick to fade out a div, replace its content, and fade it back in. The .html event is replacing the content before the fadeOut is complete...

$("#products").fadeOut(500)
              .delay(600)
              .html($("#productPage" + pageNum).html())
              .fadeIn(500);

It appears that the .html() is not being delayed by the .delay() method.

Answer

karim79 picture karim79 · Aug 13, 2010

delay will work for your case when used with the queue like this:

$("#products").fadeOut(500)
    .delay(600)
    .queue(function(n) {
        $(this).html("hahahhaha");
        n();
    }).fadeIn(500);​

Try it here: http://jsfiddle.net/n7j8Y/