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

128
Vistas
How could I make an infinite loop to show items from an array letter by letter?

I want to make an infinite loop to show msg letter by letter and item by item from an array. Can anyone help ??

let textArr = ['first', 'second', 'third'];
let i = 0;
let j = 0;

function spell() {

  if (j = 0) {
    clear();
  }

  setTimeout(function() {
    show();
    j + 0;
    if (j = textArr[i].length) {
      j = 0;
      i++;
    }
    if (i = textArr.length) {
      i = 0;
      j = 0;
    }
    spell();
  }, 1000);

}

// clear the spelling
function clear() {
  document.querySelector('#targ').innerHTML = '';
}

// show the spelling
function show() {
  document.querySelector('#targ').innerHTML += textArr[i][j];
}

addEventListener('load', spell());
<center id="targ">
  </centre>

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

0

join your array into a string, and iterate through that.

const arr = ['first', 'second', 'third'];
const targ = document.querySelector('#targ');

// Pass the array into the function
function spell(arr) {

  // `join` it up
  const str = arr.join(' ');

  // Have an inner function that you call
  // from the setTimeout initialising the index to 0
  function loop(i = 0) {

    // If the index is less than the string length
    // update the textContent of the element, and
    // increase the index
    if (i < str.length) {
      targ.textContent += str[i];
      ++i;

    // Otherwise empty the textContent,
    // and set the index to zero
    } else {
      targ.textContent = '';
      i = 0;
    }

    // Then call the loop function again with
    // the updated index as an argument
    setTimeout(loop, 200, i);
  }
  
  loop();

}

spell(arr);
<div id="targ"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

let textarr = ['first', 'second', 'third'];
let i = 0
let j = 0
let str = textarr[0]
setInterval(function() {
    i += 1
    console.log( str.slice(0,i))
    if(i == textarr[j%3].length){
    i = 0
    j += 1
    str = textarr[j%3]
    }
  }, 1000);
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