0

The example below displays only the last value. I'd like to view all of them with an interval of 2 seconds.

(2 seconds pass / value1 appears / 2 seconds pass / value 2 appears / etc...)

What am I missing?

const output = getElementById('output');       // <span>-Element to display output
const numbers = new Array(                     // array of integers
    235,
    453,
    999,
    872
);  

btn2.addEventListener('click', function(){     // foreach loop etc.
    setTimeout(function(){ 
        numbers.forEach(number => output.innerHTML = number);
     }, 2000);
});
MaxB
  • 35
  • 6
  • 1
    JavaScript doesn't pause. You can use `setTimeout()` to schedule an operation (a function call) to happen in the future. – Pointy Mar 28 '21 at 19:09
  • With either a `setInterval()` or a recursive `setTimeout()`, no loop is necessary at all and only serves to add complexity where none is needed. The reoccurrence of the timer ***is*** the loop. – Scott Marcus Mar 28 '21 at 19:28

0 Answers0