[Solved] Javascript: How to Stop Word Loop [closed]


.fadeTo() takes 3rd optional argument complete as a function to call once the animation is complete, and this is where recursion happens.

we wanted to stop the recursion when counter is just before the last element of the array

i.e. c < words.length - 1

var c = 0,
  words = ['Interesting', 'Fun', 'Exciting', 'Crazy', 'Simple'];

function loop() {
  $('h2 span').delay(1000).fadeTo(300, 0, function() {

    $(this).text(words[++c]).fadeTo(300, 1, c < words.length - 1 ? loop : null);
  });
}
loop(); // start it!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="open"> Life Can Be <span class="changeable"><b>Radical</b></span></h2>

1

solved Javascript: How to Stop Word Loop [closed]