javascript - Pausing Recursive Function Call -


i have function of i'm supposed pause on mouseenter , pause on mouseleave problem function recursive. pass in parent , index , recursively loop through each inner div displaying , hiding. function looks this:

var delay = 1000; function cycle(variable, j){     var jmax = jquery(variable + " div").length;     jquery(variable + " div:eq(" + j + ")")         .css('display', 'block')         .animate({opacity: 1}, 600)         .animate({opacity: 1}, delay)         .animate({opacity: 0}, 800, function(){             if(j+1 === jmax){                  j=0;             }else{                  j++;             }             jquery(this).css('display', 'none').animate({opacity: 0}, 10);              cycle(variable, j);         }); } 

i've tried setting timeout , clearing doesn't seem (it seems ignore timeout entirely), i've tried using stop() , calling function again on mouseout seemed repeat function call (i seeing duplicates) , stopped mid animation didn't work. tried adding in default variable @ 1 point (var pause = false || true;) couldn't work expected (though feel solution relies on variable). i'm open suggestions there rules of engagement:

rules: there can't major changes in how function works many things rely on it, it's not have control over. assume function call looks jquery('#divlist', 0) , holds bunch of div elements children.

the timeout function last solution tried looks like:

jquery('#divlist').on('mouseenter', function(){     settimeout(cycle, 100000); }) .on('mouseleave', function(){     window.cleartimeout(); }); 

perhaps this? simplified animation make example simpler, should able adapt needs.

first, have function that's responsible animating set of elements. every function call returns new function allows toggle animation (transition between pause , resume).

function startcycleanimation(els) {      var index = 0,         $els = $(els),         $animatedel;      animate($nextel());      return pausecycleanimation;      function animate($el, startopacity) {         $el.css('opacity', startopacity || 1)            .animate({ opacity: 0 }, 800, function () {                 animate($nextel());             });     }      function $nextel() {         index = index % $els.length;         return $animatedel = $els.slice(index++, index);     }      function pausecycleanimation() {         $animatedel.stop(true);          return resumecycleanimation;     }      function resumecycleanimation() {          animate($animatedel, $animatedel.css('opacity'));           return pausecycleanimation;     } } 

then can kick-start like:

$(function () {     var $animationcontainer = $('#animation-container'),         toggleanimation = startcycleanimation($animationcontainer.children('div'));      $animationcontainer.mouseenter(pauseorresume).mouseleave(pauseorresume);      function pauseorresume() {         toggleanimation = toggleanimation();     } }); 

example html

<body>     <div id="animation-container">         <div>div 1</div>         <div>div 2</div>         <div>div 3</div>     </div> </body> 

if want more generic, seems there's plugin overrides animate , allows pause/resume animations in generic way.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -