Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
Changing the value of Timeout depending on the array value

I want to print alphabet from array every 0,5 second and everytime its a vowel every 2 seconds.

I managed to write a code printing array elements every 0,5 second

var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
var i = 0;

for (let i = 0; i < alphabet.length; i++){
        setTimeout(function(){
            console.log(alphabet[i]);
            i++;
        }, 500*i)
}

How do I implement the rest

if (alphabet[i] == 'a' | 'e' | 'i' | 'o' | 'u'){
    setTimeout(function(){
        console.log(alphabet[i]);
        i++;
    }, 2000*i)
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Using Promise(), you can use the Promise.all () command, where Promise.all() waits for all Promises in the array to be processed.

function setIntervalForItem(input = [], vowels = ["a", "e", "i", "o", "u"], defaultInterval = [500, 2000])
{
  const [interval, _interval] = defaultInterval;
  var resultPromiseArray = new Array();

  for (const char of input) {
    const delay = vowels.indexOf(char) === -1 ? interval : _interval;
    resultPromiseArray.push(new Promise(resolve => {
      setTimeout(() => {
        resolve({value: char, time: delay});
      }, delay);
    }));
  }
  return Promise.all(resultPromiseArray);
}

var alphabet = [
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
];

// Then just call:
setIntervalForItem(alphabet)
  .then(result => {
    for (const object of Object.entries(result)) {
      const {value, time} = object[1];
      console.log(`The delay for character [ ${value} ] was ${time} ms`);
    }
  });

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda