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

194
Vistas
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 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