I am trying to put a name letter by letter with a delay of one second.
let nombre = document.getElementById('nombre').value; //Mario
for (let i = 0; i < nombre.length; i++) {
setInterval(function(){name.innerHTML +=nombre.charAt(i); }, 1000);
//alert(name.innerHTML);
}
What happens here is that it pauses for 1 second and then paints the name.
What I'm looking for is to put theM -> pase 1 segundo -> a -> pase 1 segundo -> r -> pase 1 segundo -> i -> pase 1 segundo -> o
When you use setInterval you don't need a loop, since what it does is execute the function repeatedly. You can use a loop with setTimeout or a single call to setInterval:
With setTimeout it would be something like:
This is another approach, derre works better for you. What I did was to implement the loop inside the setInterval, then when I complete all the letters I remove the setInterval with the timer ID.