I've been tasked with rewriting this terrible piece of code which is meant to sequentially fade in layers on a map (they are all transparent pngs) on a web page. It needs to operate in a sequence, then loop back to the start where no layers are visible, and fade back in one at a time. This sequence should repeat itself forever.
I'm not really sure what the most recommended way of doing this is in javascript and interested what stack overflow has to say.
There has to be a better way than this! Am interested in pros/cons of any methods.
setInterval(function(){
$("#layer-1").fadeIn(1000, function() {
$("#layer-2").fadeIn(1000, function() {
$("#layer-3").fadeIn(1000, function() {
$("#layer-4").fadeIn(1000, function() {
$("#layer-5").fadeIn(1000, function() {
$("#layer-6").fadeIn(1000, function() {
$("#layer-7").fadeIn(1000, function() {
$("#layer-8").fadeIn(1000, function() {
// pause for 2 seconds, then reset and hide all layers:
$("#home-map .layer").delay(2000).fadeOut();
});
});
});
});
});
});
});
});
}, 10000)
Edit: The reason I think this is different to other answers is because I was trying to set things up in an infinite loop, as well as chaining the animations. There are lots of approaches to solving callback hell in javascript and its a very common sticking point, so no doubt there will be similar questions.