Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your <code>loop</code> function looks like it will run into some problems. As soon as the loop function starts, it will keep recursively calling itself instantly putting a large strain on the browser as there isn't even a seconds break between calls.</p> <p>On top of that, as soon as any of the animations are finished, they will also additionally call the loop function again compounding the first issue. While the animations may stack correctly (one after the other on the elements you told it to animate), there is a huge amount of recursion occurring in that loop function.</p> <p>An alternative for the loop function but still getting the same effect (animation stacking) might be something like:</p> <pre><code>jQuery(document).ready(function () { var txt1Animate = function() { jQuery("#txt1").animate({ backgroundColor: "#0089BC" }, 3000, function(){ jQuery(this).animate({ backgroundColor: "#FFF" }, 3000, txt1Animate); }); } var txt2Animate = function() { jQuery("#txt2").animate({ backgroundColor: "#0089BC" }, 9000, function(){ jQuery(this).animate({ backgroundColor: "#FFF" }, 9000, txt2Animate); }); } var txt3Animate = function() { jQuery("#txt3").animate({ backgroundColor: "#0089BC" }, 6000, function(){ jQuery(this).animate({ backgroundColor: "#FFF" }, 6000, txt3Animate); }); } txt1Animate(); txt2Animate(); txt3Animate(); }); </code></pre> <p>This would now correctly wait for the first animation to finish on the element, then do the second and only once the second is complete will it start again. This stops the animation queue from spiraling out of control with 100s to 1000s of animations being queued a second.</p> <p>Addressing your second part of the question, you are calling <code>fadeOut</code> and then straight away calling <code>fadeIn</code>. While this should queue that animation, that goes for 12 seconds and then you have it already being called again looped by <code>setInterval</code>. This would also create issues in the long term with the queue growing every 10 or so seconds without any chance of finishing.</p> <pre><code>jQuery(document).ready(function () { jQuery("#img1").hide().fadeIn(3000); var img1Fade = function() { jQuery("#img1").fadeOut(3000).fadeIn(3000,function() { setTimeout(img1Fade,5000); }); } jQuery("#img2").hide().fadeIn(9000); var img2Fade = function() { jQuery("#img2").fadeOut(9000).fadeIn(9000,function() { setTimeout(img2Fade,5000); }); } jQuery("#img3").hide().fadeIn(6000); var img3Fade = function() { jQuery("#img3").fadeOut(6000).fadeIn(6000,function() { setTimeout(img3Fade,5000); }); } img1Fade(); img2Fade(); img3Fade(); }); </code></pre> <p>Similar to the other code I provided, this loops on itself in a way that won't have a huge queue of animations at once. It nicely waits for the fade in to finish before running <code>setTimeout</code> to loop the function again.</p> <p><strong>EDIT</strong></p> <p>Picked up one bug in the code I supplied originally, I was using <code>$</code> for jQuery however in your code on the site you linked, I required to do <code>jQuery</code> instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload