5

I have an array with predefined lines:

    var linesArr = ["asd", "dsa", "das"];

I have a div, which i created with JS and styled it with my CSS:

    var div = document.createElement("div");
    div.className = "storyArea";
    div.innerHTML = linesArr[0];

Right now i have that code that can animate the texts fadeIns and fadeOuts on click:

    $(div).click(function(){
    $(this).fadeOut(1000, function() {
       $(this).text("Random text").fadeIn(2000);
        });
    });

But it is not a cycle that can iterate through my array items, it will be showing the predefined text all the time.

I tried to write a cycle that can work through that, but got lost:

    $(div).click(function(){
    for (var i = 1; i < linesArr.length; i++) {
        $(div).fadeOut(1000, function() {
            $(this).html(linesArr[i].fadeIn(2000));
            });
        };
    });

That cycle is not working, i don't have any console errors, but the logic here is flawed. Can somebody help me?

mplungjan
  • 169,008
  • 28
  • 173
  • 236

3 Answers3

2

Do you want like this

var linesArr = ["asd", "dsa", "das"];

var div = document.createElement("div");
    div.className = "storyArea";
    div.innerHTML = linesArr[0];
    document.body.appendChild(div);
    
    $(div).click(function(){
    //for (var i = 1; i < linesArr.length; i++) {
        $(div).fadeOut(1000, function() {
            index = linesArr.indexOf($(this).html()) + 1;
            $(this).html(linesArr[index % linesArr.length]);
            $("div").fadeIn(2000);
            });
        //};
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
1

You can use a pointer (show in this case) to keep track of the current element being shown. Then on each click fade out the current and fade in the next.

I'm also using a module % to loop back to the first element when you reach the end.

var linesArr = ["asd", "dsa", "das"];
var show = 1;

var div = document.createElement("div");
div.className = "storyArea";
div.innerHTML = linesArr[0];

$(document.body).append(div);

$(div).click(function() {
  $(div).fadeOut(1000, function() {
    $(this).html(linesArr[show++ % linesArr.length]).fadeIn(2000);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
0

If you need a cycle, you can use a loop in an IIFE:

 $('div').click(function () {
  (function loop(i) {
    $('div').html(linesArr[i]).fadeOut(1000, function () {
        $(this).html(linesArr[i]).fadeIn(2000);
        i++;
        if (i < linesArr.length) {
            loop(i);
        }
    });
  })(0);
});

var linesArr = ["asd", "dsa", "das"];
$('div').click(function () {
    (function loop(i) {
        $('div').html(linesArr[i]).fadeOut(1000, function () {
            $(this).html(linesArr[i]).fadeIn(2000);
            i++;
            if (i < linesArr.length) {
                loop(i);
            }
        });
    })(0);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div>Click Me</div>

When the complete callback runs your i variable already reached the last value. In order to preserve the variable scope you can use IIFE

gaetanoM
  • 41,594
  • 6
  • 42
  • 61