Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce nested callbacks in javascript/jquery

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.

like image 616
mike-source Avatar asked Apr 27 '17 17:04

mike-source


People also ask

Are callbacks faster than promises?

It's a long debated hot topic between javascript developers what is efficient or fast. Till now common perception was that for getting out of callback hell we should use promises or for clean code but what about efficiency. So from my findings i assure you ES6 promises are faster and recommended than old callbacks.

Why promises are better than callbacks medium?

Difference between callback and promises As explained above, promises are cleaner way for running asynchronous tasks to look more like synchronous and also provide catching mechanism which are not in callbacks. Promises are built over callbacks.

How are promises more superior than callbacks?

They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.


1 Answers

use a class instead of ids, then loop through them and add delay based on their index

var layers = $(".layer").length;

function foreverLoop() {

  $(".masterLayer").show();
  $(".layer").hide();

  $(".layer").each(function(index) {
    $(this).delay(1000*index).fadeIn(1000);
  });
  
  $(".masterLayer").delay(1000*layers + 2000).fadeOut(1000);
  
  setTimeout("foreverLoop()", 1000*layers + 3000 + 500);
}

foreverLoop();
.layer {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="masterLayer">
  <div class="layer">layer 1</div>
  <div class="layer">layer 2</div>
  <div class="layer">layer 3</div>
  <div class="layer">layer 4</div>
  <div class="layer">layer 5</div>
  <div class="layer">layer 6</div>
</div>
like image 69
indubitablee Avatar answered Oct 13 '22 12:10

indubitablee