I have an array, and I want each element to come out every X time, but I can't do it, I get the whole array directly
var miarray = "Ejemplo";
miarray = Array.from(miarray);
miarray.forEach(element => {
setInterval(function(){ console.log(element);}, 3000);
});
I had understood that element = a letter and it iterates, but everything comes out directly, how would I make it go from 1 to 1?
You could make use of the setTimeout method which evaluates the expression after the time you specify:
Code :
With your code, it would be the following:
For each element to come out, you have to Multiply the work index at the same time, this solves it, and use the function
setTimeout
:Theoretically I think that the foreach also encapsulates the time and when setTimeOut is executed and checks the time that has elapsed in each iteration, the result is that the time has already passed; that's why you have to multiply it by the index.