You can create a method and use setInterval
, see:
let li = document.querySelectorAll('li');
let x = 0;
let intervalo = null;
const loop = () => {
if (x < li.length) {
console.log(li[x].innerText);
} else {
clearInterval(intervalo);
console.log('Fim !');
x = 0;
}
x++;
};
intervalo = setInterval(loop, 1000);
<li>Texto A</li>
<li>Texto B</li>
<li>Texto C</li>
<li>Texto D</li>
<li>Texto E</li>
<li>Texto F</li>
<li>Texto G</li>
<li>Texto H</li>
<li>Texto I</li>
The loop
method will be executed every 1 second, which will check if the value of the x
variable is less than the number of li
elements in the document, if it returns the text of that element in the console