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

124
Vistas
How to use several setTimeout one by one?

I would like to animate text decryption. For ex., I have the encrypted text **** long ********* **** and I would like to slowly replace it with some long encrypted text.

I tried to use the code below, but it replaces the text immediately when I need to pause after each symbol is replaced.

function play(encText, decrText) {

    function showText() {
        var text = decrText.substring(0, i+1) + encText.substring(i+1);
        console.log(text);
        document.getElementById('text').innerHTML = text;
    }

    for( var i=0; i < encText.length+1; i++ ) {
        setTimeout( showText(), i*5000 );
    }

}

See https://jsfiddle.net/bwf0Layg/.

How could I fix that?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can create a closure for you showText on your i loop, by returning a function.

Also, I've changed you innerHTML to innerText, to stop some funky things happening, and decreased the time from 5 seconds for demo purposes.

eg.

function play(encText, decrText) {
    function showText(i) {
       return () => {
         var text = decrText.substring(0, i + 1) + 
            encText.substring(i + 1);
         document.getElementById('text').innerText = text;
       }
    }

    for (var i = 0; i < encText.length + 1; i++) {
       setTimeout(showText(i), i * 100);
    }
}

play(document.getElementById('text').innerText, 'some long encrypted text')
<div id="text">
**** long ********* ****
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem with your code is that the last run writes all the letters at once. What you could do is create a sleep function with a promise and setTimeout

const sleep = ()=> new Promise((resolve)=> setTimeout(resolve,5000));

async function play(encText, decrText) {
      function showText(i) {
        var text = decrText.substring(0, i + 1) + encText.substring(i + 1);
        console.log(text);
        document.getElementById('text').innerHTML = text;
      }

      for (let i = 0; i < encText.length + 1; i++) {
        await sleep();
        showText(i)
      }

    }

so on every iteration, your code "sleeps" or awaits for 5 seconds before moving to the next iteration making this slow letter appearance.

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