JS setInterval() help

Thanks, I’m going to dig into both setTiming and setInterval a little more so this will come in handy!

As already shown you can just increment an index variable inside setInterval, I don’t really see why you would need a loop (unless I’m missing something with the use case).

const arr = ["1", "2", "3", "4", "5", "6"];
let index = 0;

const output = document.getElementById("output");

setInterval(() => {
  if (!arr[index]) {
    index = 0;
  }
  output.textContent = arr[index];
  index++;
}, 2000);
1 Like