Wednesday, February 1, 2012

Lesson 3: Callbacks

Last time we spoke of stacking animations, that was fun. But what if you want to start doing something with another element when some animation is finished? Asynchronous programming comes in handy not only when calling server side. Let's see how callbacks work for jQuery animations.

Just a few lines of code and we have a MIDI-keyboardish thingie full of colored square keys to click: Yes, they are clickable!

Pretty much done already, could employ a couple of swell modern features though. What kind of a midi-keyboard cannot do anything on it's own? Playing sequences after just one touch is a must-have feature, time to code it! Pressing one key will start off a bunch of pitches, as if we pressed some of them sequentially. When launching an animation on a key, we need to know which one goes next. We'll link elements using their data field. Have a look at jQuery .data, nice one. Now that for each element we know who's next, we can enhance the animation function by adding a callback. Callback practice is over, enjoy the result of all this hard work :)

Source code links:

Move on to the next lesson!