I’ve been searching for quite a while now, and before everyone hits me the setTimeout() function, hear me out.
I have ~20 HTML elements which are invisible. I want them to turn visible one at a time, with a pause inbetween. I also want to use some of jQueries awesome animation features when turning them visible.
So basically I want to “play a sequence of frames/events/states” using JavaScript/jQuery.
What have I looked in to?
SetTimeout():
I’ve been looking at the JS setTimeout function, and from what I can tell, it works great when you have 1-3 setTimeout() functions.
If you want more, it starts to get extremely messy. They are embedded, you have to time everything by adding numbers together and it’s really hard to get the overall picture.
jQuery.delay()
Also, the jQuery.delay() function lacks to actually pause the code. I have 20+ elements, not one I want to animate.
Updatepanel spam
Another option, which I kind of considers right now (please, kill me), is using an updatepanel. Every second it makes a postback using a timer, and using a session, it knows exactly at what “frame” it is. Then it can set things visible/invisible depending on frame number, and also fire jQuery animations.
Now, when I woke up this morning, I didn’t dream of spamming my webserver with an updatepanel like this…. So:
Is there any good ways to do this? I am very open to suggestions.
Thanks a lot! 🙂
If you apply the same class to all the elements (
sequential_exposein my example), you can select those elements and calleach()on the selection. To stagger the fade in of the elements, you can multiple the index of the item as provided fromeach()and multiply it by the time you want for for the delay between fadeIn’s and pass that value as parameter todelay(). Given an interval of 500 milliseconds (you can make this whatever you want), the first item in selection would have0as delay, the second500, the third1000, etc. You then just chain your preferred animation (fadeIn(200)in my example) after the call todelay().Note the example shows this as a document onready function, you can obviously change this as needed.