callback setTimeout

1

I'm doing a practice with callback functions what I want is that for every loop that the loop has a waiting time of 2 seconds I have an array with letters what I try to do is print the letters of it every 2 seconds .

I am trying to do with the setTimeout method but I do not get the expected result. You could give me a support with this exercise please.

Greetings.

var array1 = ['a', 'b', 'c'];

function llamadora(array1,callback){

callback(array1);

}

var ejecutora = (function(array1){

array1.forEach(function(element) {

setTimeout(console.log(element),2000);

});

})

llamadora(array1, ejecutora)
    
asked by Rangel 27.07.2018 в 02:26
source

1 answer

1

I greet you and I propose this solution

let array1 = ['a', 'b', 'c'];

const imprime = () => {
 setInterval(function(){
   console.log(array1)
 }, 2000); 
}

imprime()

CLARIFICATIONS

  
  • You should not use setTimeOut() since that instruction will only be executed once given the time you set
  •   
  • Instead a setInterval() that will execute the assigned code within the amount of time you assign it in a way   indefinite
  •   
  • Just modify the syntax a bit using an arrow function assigned to the constant print
  •   

    ANNEX

    In the same way you can with the map() method you can go through the original arrangement and a new one to deliver the arrangement as you want to show it, that is, print the elements every 2 seconds; so look:

    let array1 = ['a', 'b', 'c'];
    
    const imprime = () => {
     setInterval(() => {
       array1.map(ele => {
         console.log(ele)
       })
     }, 2000); 
    }
    
    imprime()
    
        
    answered by 27.07.2018 в 02:40