Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery delay() works after effect, but not before?

Tags:

jquery

delay

I'm showing a line of text using a typewriter effect [http://plugins.jquery.com/project/jTypeWriter].

I want to:
1) Delay 3 seconds
2) Show typewriter text
3) Delay 4 seconds
4) Fade out

But step 1 doesn't happen with this code: $('blockquote').delay(3000).jTypeWriter({duration:1}).delay(4000).fadeOut();

Why does delay() not work at the beginning?

like image 825
thv20 Avatar asked Dec 12 '25 00:12

thv20


2 Answers

delay() only works within an animation queue - so yes, an animation needs to be called before it will work as you expect.

setTimeout() is probably the way to go:

var t = window.setTimeout(function(){
    $('#myDiv').jTypeWriter({duration:1}).delay(4000).fadeOut();
}, 3000);
like image 175
John McCollum Avatar answered Dec 13 '25 18:12

John McCollum


To use .delay(), you would need to add the jTypeWriter() to the animation queue. You can use jQuery's .queue() method for this:

$('blockquote').delay(3000)
               .queue(function( n ){ $(this).jTypeWriter({duration:1}); n(); })
               .delay(4000)
               .fadeOut();

Calling the n() parameter in the .queue() is used to allow the next element in the queue to continue.

  • http://api.jquery.com/queue/
like image 40
user113716 Avatar answered Dec 13 '25 18:12

user113716



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!