Using jQuery delay() with separate elements
Posted
by boomturn
on Stack Overflow
See other posts from Stack Overflow
or by boomturn
Published on 2010-03-02T13:59:51Z
Indexed on
2010/04/30
3:57 UTC
Read the original article
Hit count: 443
I want to fake a simple animation on page load by sequentially revealing several pngs, each appearing instantly but with unique, precisely timed delay before the next one. Using the latest version of jQuery (1.4.2 at writing), which provides a delay method. My first (braindead) impulse was:
$('#image1').show('fast').delay(800);
$('#image2').show('fast').delay(1200);
$('#image3').show('fast');
Of course all come up simultaneously. jQuery doc examples refer to a single item being chained, not handling multiple items. OK... try again using nested callbacks from show():
$('#image1').show('fast', function() {
$('#image2').show('fast', function() {
$('#image3').show('fast', function() {
});
});
});
Looks ugly (imagine doing 10, also wonder what processor hit would be) but hey, it works sequentially! Without delay though. So... chain delay() after show() using a callback function from it (not even sure if it supports one):
$('#image1').show('fast').delay(800, function() {
$('#image2').show('fast').delay(1200, function() {
$('#image3').show('fast');
});
});
Hm. Opposite result of what I thought might happen: the callback works, but still no delay. So... should I be using some combination of queue and delay instead of nested callbacks? jQuery doc examples for queue again only use one element for example. Guessing this would involve using a custom queue, but am uncertain how to implement this. Or maybe I'm missing a simple way? (For a simple problem!)
© Stack Overflow or respective owner