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

192
Visualizações
JavaScript - Local/Global variables: How can I share values inside lamda-functions

I'm trying to understand, how variables in javascript work. Here is my code:

textarea.addEventListener('keyup', (e) => {
   singleChoices.innerHTML = textarea.value
      .split(',')
      .filter((eintrag) => {
         return eintrag.trim() !== ''
      })
      .map((choice) => {
         return `<span class="choice">${choice.trim()}</span>\n`
      })
      .join('')

   if (e.code === 'Enter') {
      textarea.value = ''

      const choice = getRandomChoice()
      
      const repeat = setInterval(() => {
         blinkOn(choice)
         const verzoegerung = setTimeout(() => {
            blinkOff(choice)
         }, 30)
      }, 30)

      const stopRepeat = setTimeout(() => {
         clearInterval(repeat)
         blinkOn(choice)
      }, 2000)
   }
})

function getRandomChoice() {
   const choices = document.querySelectorAll('.single-choice .choice')
   let randomIndex = Math.floor(Math.random() * choices.length)
   return (choice = choices[randomIndex])
}

Im trying to create a blink-effect. BlinkOn adds a class with a new color to a random span (called choice), blinkOff is removing this class again.

My Question: After two seconds I want to keep the last random index and call again blinkOn ON that index. Short: I need to use the same value of the random index in stopRepeat part. How do I have to define the variables? At this moment I didnt get it!

about 4 years ago · Juan Pablo Isaza
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