1

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 />

3
  • You should read about closure in javascript. Commented Nov 24, 2019 at 14:00
  • I would assume he's wanting to change the title every second, looping through the array Commented Nov 24, 2019 at 14:01
  • Try this. its work for me.. title.textContent += word[i]; demo: jsfiddle.net/Danielprabhakaran_N/sjzrx3qy Commented Nov 24, 2019 at 14:24

4 Answers 4

2

For a small amout of intervals, I suggest to use setTimeout instead and given them appropriate timeouts and a closure over the wanted character to add to innerHTML of the element.

function loop(word, target) {
    for (let i = 0; i < word.length; i++) {
        setTimeout((c => () => target.innerHTML += c)(word[i]), (i + 1)* 1000);
    }
}

loop('hello', document.querySelector('h1'));
<h1></h1>

If you still want to use setInterval, you need to store the interval and clear this interval at the end of the word.

function loop(word, target) {
    var interval = setInterval((i => () => {
        if (i >= word.length) return clearInterval(interval);
        target.innerHTML += word[i++];
    })(0), 1000);
}

loop('hello', document.querySelector('h1'));
<h1></h1>

Sign up to request clarification or add additional context in comments.

Comments

1

This is because you're doing the interval inside the loop, that means by the time you get to your 1s the i is already at the end of your array i = 4.

I think this is what you want?

let i = 0;
let interval;

const loop = () => {
  console.log(word[i]);
  if (word[i]) title.textContent += word[i];
  else clearInterval(interval)
  i ++;
}

interval = window.setInterval(loop, 1000);

Hope this helps

Comments

1

If you want to change your title after 1 second for all the words in array you can start with index 0 and keep on setInterval till your index is less than word.length.

Like:

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

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

loop(0);

function loop(index) {
  setTimeout(() => {
    if (index < word.length) {
      title.textContent = word[index];
      setTimeout(() => loop(index+ 1), 1000);
    } else {
    setTimeout(() => loop(0), 1000);
    }
  }, 1000)
}

Here I am setting the first element, and doing setTimeout for the next one

if index has exceeded the word length I have put else as well to reset the word loop.

Comments

1

Here is my understanding. This program should append hello for every one second.

This code works for me

html

<h1 />
<h2 />

js

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();

demo:

https://jsfiddle.net/Danielprabhakaran_N/sjzrx3qy/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.