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

176
Views
javascript cannot remove eventListener. (ts)

index.ts

if(audio.paused) {
    audio.play()

    audio.addEventListener('timeupdate', (e) => handleAudioPlayer(<HTMLAudioElement>e.target, 
        <HTMLDivElement>audio.parentElement), true);
}

else {
    audio.pause()

    audio.removeEventListener('timeupdate', () => handleAudioPlayer, true);
}

Basically I have a handleAudioPlayer function that takes 2 arguments, when the audio is playing the event listener is added, and when its paused it should get removed, but here the eventListeners are getting duplicated which is bad, any help is appreciated!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're trying to remove a different function than you added in the first place. Instead, try to add and remove the same handler.

const handler = (e) => handleAudioPlayer(<HTMLAudioElement>e.target, 
        <HTMLDivElement>audio.parentElement)
audio.addEventListener('timeupdate', handler, true)
audio.removeEventListener('timeupdate', handler, true)
about 4 years ago · Juan Pablo Isaza Report

0

In your case you should declare your function to a variable. Then you can remove the eventListener.

if(audio.paused) {
  audio.play()

  audio.addEventListener('timeupdate', handleAudioPlayer, true);
}

else {
  audio.pause()

  audio.removeEventListener('timeupdate', handleAudioPlayer, true);
}

let handleAudioPlayer = (evt) => {
 // do something
};
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!