rwu6
rwu6

Reputation: 83

How to loop changing text in HTML/Javascript?

How would I use HTML/Javascript to make text change, looping through a predetermined sequence?

Kind of like this, but in text form, instead of GIF

Thanks!

Upvotes: 1

Views: 14407

Answers (1)

user5801980
user5801980

Reputation:

    <script>
        var example = ['A', 'B', 'C', 'D'];

        textSequence(0);
        function textSequence(i) {

            if (example.length > i) {
                setTimeout(function() {
                    document.getElementById("sequence").innerHTML = example[i];
                    textSequence(++i);
                }, 1000); // 1 second (in milliseconds)

            } else if (example.length == i) { // Loop
                textSequence(0);
            }

        }
    </script>

And the div to show the sequence:

<div id="sequence"></div>

Upvotes: 8

Related Questions