javascript - Optimising jQuery callbacks -


i have function works want too, looks messy , bloated , im wondering, there better way code below?

function careerflyin(){      var distance = $('.row').offset().top,         $window = $(window);     var distance = distance - 200;     var flyinr = $('.path-description');     var flyinl = $('.path-title');      $window.scroll(function() {         if ( $window.scrolltop() >= distance ) {             $('.career-path .row:first-child').find(flyinl).animate({ 'left' : '0px' } ,400, 'easeoutbounce', function () {                   $('.career-path .row:nth-child(2)').find(flyinl).animate({ 'left' : '0px' } ,400, 'easeoutbounce', function () {                      $('.career-path .row:nth-child(3)').find(flyinl).animate({ 'left' : '0px' } ,400, 'easeoutbounce', function () {                          $('.career-path .row:nth-child(4)').find(flyinl).animate({ 'left' : '0px' } ,400, 'easeoutbounce', function () {                              $('.career-path .row:nth-child(5)').find(flyinl).animate({ 'left' : '0px' } ,400, 'easeoutbounce', function () {  });                          });                     });                 });                          })             $('.career-path .row:first-child').find(flyinr).animate({ 'right' : '0px' } ,400, 'easeoutbounce', function () {                   $('.career-path .row:nth-child(2)').find(flyinr).animate({ 'right' : '0px' } ,400, 'easeoutbounce', function () {                      $('.career-path .row:nth-child(3)').find(flyinr).animate({ 'right' : '0px' } ,400, 'easeoutbounce', function () {                          $('.career-path .row:nth-child(4)').find(flyinr).animate({ 'right' : '0px' } ,400, 'easeoutbounce', function () {                              $('.career-path .row:nth-child(5)').find(flyinr).animate({ 'right' : '0px' } ,400, 'easeoutbounce', function () {  });                          });                     });                 });                          })         }     });  }; 

create list of items animate , use async recursion on list.

function animate(elements, callback) {     if (elements.length){         elements.eq(0).find(flyinr).animate({ 'right' : '0px' }, 400, 'easeoutbounce');         elements.eq(0).find(flyinl).animate({ 'left' : '0px' }, 400, 'easeoutbounce', function (){             // remainder of list (after first item)             animate(elements.slice(1), callback);         });     }     else {         // done, call final callback         callback();     } }  animate($('.career-path .row'), function()  {     // when items have finished animating }); 

you can apply pattern set of similar async operations. in example left , right animations run in parallel 1 triggers next event (left in case).


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -