Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 455
Derek Adair Avatar asked Aug 13 '10 01:08

Derek Adair


People also ask

What is the use of delay () method in jQuery?

jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.

Is delay event method in jQuery?

The delay() is an inbuilt method in jQuery which is used to set a timer to delay the execution of the next item in the queue. para1: It specifies the speed of the delay. para2: It is optional and specifies the name of the queue.

What is delay () in Javascript?

Conclusion. setTimeout() is a method that will execute a piece of code after the timer has finished running. let timeoutID = setTimeout(function, delay in milliseconds, argument1, argument2,...); The delay is set in milliseconds and 1,000 milliseconds equals 1 second.


2 Answers

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/

like image 89
karim79 Avatar answered Oct 09 '22 01:10

karim79


Maybe the "queue" way it's ok, But this javascript solution works better for me:

    setTimeout (function(){       $("#products").html('Product Added!');     },1000); 
like image 41
gtamborero Avatar answered Oct 09 '22 01:10

gtamborero