Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Cómo agregar correctamente setInterval/setTimeout a mi función

¡Por favor, ayuda!

La función debe imprimir las letras "A" a "F" en la consola, una letra por segundo.

La función funciona sin setInterval/setTimeout.

 // setInterval(alphabet, 1000, 'a', 'f'); function alphabet(from, to) { var a = [], i = from.charCodeAt(0), j = to.charCodeAt(0); for (; i <= j; ++i) { setInterval(() => { a.push(String.fromCharCode(i)); }, 1000); } // return console.log(a); return a; } console.log(alphabet('a', 'f'));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

 function printLetters(start, finish) { //should add checks here to validate start comes before finish console.log(start) if (start == finish) { return } else { setTimeout(printLetters, 1000, String.fromCharCode(start.charCodeAt(0)+1), finish) } } printLetters('a', 'f')

about 4 years ago · Juan Pablo Isaza Relatório

0

Es posible que desee probar esto. Puedes pegarlo en codepen

 var a = []; function alphabet(from, to) { console.log(a); setInterval(() => { var i = from.charCodeAt(0); var j = to.charCodeAt(0); for (; i <= j; ++i) { if(a.includes(String.fromCharCode(i))){ }else{ console.log(String.fromCharCode(i)) a.push(String.fromCharCode(i)); break; } } }, 1000); return a; } alphabet('a', 'f');
about 4 years ago · Juan Pablo Isaza Relatório

0

Aquí hay una solución que usa una función de suspensión async .

 function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function alphabet(from,to){ var currentLetter = from[0]; while(currentLetter.charCodeAt(currentLetter[0]) != to.charCodeAt(to[0])+1){ let l = currentLetter await sleep(1000) console.log(l) nextLetter = String.fromCharCode(currentLetter.charCodeAt(currentLetter[0])+1) currentLetter = nextLetter; } } alphabet('A','F');

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda