Actually, I just missed the immediate execute () – without which it doesn’t make a whole lot of sense. That could be what the OP is missing as well on this… when you }() at the end of a function, it means ‘execute this function immediately as an object’ – the return then providing the function that is actually run. For some bizzare reason when you do that the object is not disposed and any vars you declare in it are preserved – indefinately. (basically until the page is unloaded) – this is why it’s dangerous to make that type of call inside a loop. (I’ve seen people do it… wondering why it crashes the browser after a few minutes).
Normally I attach properties to be passed to the parent element… along with any such functions. It adds to the namespace, but at least it’s down-tree addition in the DOM reducing the odds of conflicts.
for example:
var parent=document.getElementById('latest');
parent.aAll=parent.getElementsByTagName('a');
parent.aIndex=0;
parent.aAll[0].style.display='inline';
parent.aIntervalHandler=function() {
with (this) {
aAll[aIndex].style.display='none';
aIndex=(aIndex++)%aAll.length;
aAll[aIndex].style.display='inline';
}
}
Which in “let’s tack a fatass bloated library to it” would be:
var parent=$('#latest');
parent.aAll=$('#latest a');
parent.aIndex=0;
parent.aAll.eq(index).show();
parent.aIntervalHandler=function() {
with (this) {
parent.aAll.eq(index).hide();
aIndex=(aIndex++)%aAll.length;
parent.aAll.eq(index).show();
}
}
I can see the advantage of using a self-constructing object’s namespace to handle it if you’re only calling this once – though said object (and at that point the outer function is an object) never releasing itself from memory means you’re relying HEAVILY on the browsers garbage collection. (Meaning train wreck in FF). Still, the biggest issue I see is constantly calling the list selection INSIDE the function. If you can preserve index between them, you should be able to preserve ALL as well.
setInterval((function() {
var
index=0,
all=document.getElementById('latest').getElementsByTagName('a'),
all[0].style.display='inline';
return function() {
all[index].style.display='none';
index=(index++)%all.length;
all[index].style.display='inline';
}
}(),1000);
Or with the idiotic bloated needlessly cryptic library that flushes 90%+ of the websites it’s used on…
setInterval((function() {
var
index=0,
all=$('#latest a'),
all.eq(0).show();
return function() {
all.eq(index).hide();
index=(index++)%all.length;
all.eq(index).show();
}
}(),1000);
Which is a pretty slick way of handling it… All those unnecessary conditionals and calling up the list on every interval seemed needlessly complex. Coding 101, do as little inside the loop as possible. This way also means you’ll have a link showing BEFORE the second of the first interval is up. This does have the issue that if you’re adding/removing those anchors via scripting it will fail on you though… but since they are anchors that means they are content – which means you have little if any business adding/removing them with scripting in the first place.