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

141
Views
Prevent addEventListener() run

I have a function that runs speech recognition when I click button start, when speech recognition is over some actions should be started. I use addEventListener('audioend', ()=>...) to track the event. Everything work as expected, but now I am adding another functionality to stop listening when I click the same button and the function should be completed also. But I faced an issue with addEventListener that runs despite the return key word.

const recognition = new SpeechRecognition.getRecognition()
const [isPlaying, setIsPlaying] = useState(false)

  function playSong() {
    setIsPlaying(!isPlaying) //when I click 'Play' button again it changes to 'Stop'
    if (listening) { //verify if speech recognition listening 
      SpeechRecognition.stopListening()
      console.log('Should stop here') 
      return   //I try to use return but it doesn't help, in any case addEventListener runs next
    }else if {
      recognition.addEventListener('audioend', () => {
      console.log('Some other actions')
    }
   ........
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of return you need to remove the event listener

const dosomeaction = () => {

    console.log("do some action")
};
recognition.addEventListener('audioend', dosomeaction)

In this way you add the event listiner so you can remove it by this way

recognition.removeEventListener('audioend', dosomeaction)
about 4 years ago · Juan Pablo Isaza Report
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!