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

245
Visualizações
Splitting text area value into array then loop and run function

I'm attempting to:

  • Take the input from a text area
  • Split into an array of strings, each line being an element in that array
  • Loop through each element this array, running a function that uses the text in the current element, then wait 5 seconds before going into the next loop iteration/element.

All my attempts so far have unfortunately not worked, I'm a beginner to JS so am taking snippets and trying my best here:

// var LS = document.querySelector('textarea').value().split('\n');
for (let i in LS) {
   setTimeOut(function(i),5000);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

textInput = document.getElementById("theText");
textArray = textInput.value.split(/\n/g);




const delayLoop = (fn, delay) => {
  return (line, i) => {
    setTimeout(() => {
      useTheText(line);
    }, i * 5000);
  }
};



function useTheText(line){
  console.log(line);
}

const handleClick = () => {
textInput.value.split(/\n/g).forEach(delayLoop(useTheText, 500));

}
<textarea id="theText" name="theText"
          rows="5" cols="33">
It was a dark and stormy night...
second line
line 3
</textarea>

<div>
<button onClick="handleClick()">process</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

The first argument to setTimeout() must be a function to call. You're calling the function immediately.

If you want them to run 5 seconds apart, multiply the timeout value by i. The timeout is measured from when you call setTimeout, not when the previous function ran.

for (let i in LS) {
    setTimeout(() => yourFunction(LS[i]), i * 5000);
}
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