A
A
Alexander2020-05-10 16:43:09
JavaScript
Alexander, 2020-05-10 16:43:09

Why is the JS handler not working properly?

I am not strong in JS, I read the guide on event handlers and wrote the code:

var audio = document.getElementById("musicPlayer");
audio.addEventListener('ended', function () {
  nextPlayMusic();
})

When I press pause, I clean the event:
var audio = document.getElementById("musicPlayer");
audio.removeEventListener("ended", function() {});

But after some time of pause, nextPlayMusic(); from the event...
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
its2easyy, 2020-05-10
@SmoKE_xD

You need to delete the same function that was added, and an anonymous function is created every time a new one, so

audio.addEventListener('ended', nextPlayMusic);
audio.removeEventListener('ended', nextPlayMusic);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question