looping through an array of js and then ending on a single value permanently

Use setTimeout to schedule a single recursive call, not setInterval:

var wordCycle = [
    'Panierte Schuhsohlen',
    'Essbare Sitzbezüge', 
    'Häuser wie Bäume',
    'Städte wie Wälder',
    'Ein neues Bio', 
    'Masken als Nährstoffe'
];

textSequence(0);
function textSequence(i) {

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

    } 
    else if (wordCycle.length == i) {
        document.getElementById("sequence").innerHTML = 'Umdenken.';
    }
}
<div id="sequence"></div>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top