Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debunking .delay()

Tags:

jquery

Please see Matt's answer to this question. He says that .delay() is confusing to most people, and he gives the following example:

$('#foo').hide().delay(2000).slideDown().text('Hello!').delay(2000).hide();

See here for the fiddle. Could somebody explain the behaviour of that line of code?

like image 266
Randomblue Avatar asked Feb 12 '26 08:02

Randomblue


1 Answers

Only certain parts of jQuery's code can be pushed off with .delay() -- hide() and text() are not a part of that group.

So basically what the code is doing is (not really doing this, this is just a sketch):

setTimeout(function(){
    $('#foo').slideDown();
}, 2000);
$('#foo').hide().text('Hello!').hide();

So that is why the slideDown() seems to happen last.

like image 88
Naftali Avatar answered Feb 15 '26 00:02

Naftali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!