Hiding with recursion - functional style
In this case you know the duration of the effect, so you can do as others suggested.
However you might still want to know how to achieve the same using a more functional style, using a simple recursion.
So here I show you a way to do that.
1. The code
<script src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
$(function() {
var items = jQuery(".to-hide");
(function hideRec() {
if (items.length == 0) {
window.alert("The end.");
return;
}
var toHide = jQuery(items[0]);
items = items.slice(1);
toHide.hide("100", hideRec);
})();
});
<script>
<ul>
<li class="to-hide">...</li>
<li class="to-hide">...</li>
<li class="to-hide">...</li>
<li class="to-hide">...</li>
<li class="to-hide">...</li>
<ul>
2. How does it work?
We first obtain, from jQuery, an array containing the objects we want to-hide
.
Then we create a named anonymous function that will be executed right after its definition -- it's the (function() { ... })();
pattern. Even if it is an anonymous function, we give it a name so that we are able to easily call it recursively.
Note that you can achieve the same thing without giving an anonymous function a name -- which may sound a little strange if you don't understand how JavaScript deals with scope -- with the more obscure version:
(function() { // Anonymous function without a name
if (items.length == 0) {
window.alert("The end.");
return;
}
var toHide = jQuery(items[0]);
items = items.slice(1);
toHide.hide("100", arguments.callee); // Recursive call to anonymous function
})();
This time we used the fact that arguments.callee
represents the function itself -- so no need to give an anonymous function a name.
The whole magic is inside this recursive anonymous function -- that is a closure.
The hideRec
anonymous function captures the items
array. After checking that there's still something inside, it will remove the first element and store it in the local toHide
variable (properly wrapped with jQuery).
At the end, it uses jQuery to hide the element, and passes this anonymous function as the callback -- jQuery will call it again once it has finished the effect.
I hope this is clear enough.
Good luck!