Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!