Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138221

Loop over array with setInterval doesn't work

$
0
0

The problem is that now, loop shows only last value of array which is 'o'

const h2 = document.querySelector('h2');
const title = document.querySelector('h1');

const word = ['h', 'e', 'l', 'l', 'o'];

function loop() {
    for(let i = 0; i < word.length; i++) {
        window.setInterval(() => {
            console.log(word[i]);
            title.textContent = word[i];
        }, 1000)
    }
}

loop();
<h1 /><h2 />

Viewing all articles
Browse latest Browse all 138221

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>