Removing eventListeners?

Hello,

This is my first Twitch developer question, hope I’m doing this right. I am embedding several channels on my web page and I’m currently listening for the Twitch.Player.ONLINE eventListener in order to do something with that stream video. Question is: Once I’m able to call my function from the event I want to go ahead and remove the event listener, because I don’t want it being called anymore. Is that a possibility? My code looks something like this:

player.addEventListener(Twitch.Player.ONLINE, function() {
var channelName = player._bridge._playerState.channelName;
// do something with the channelName
player.removeEventListener(Twitch.Player.ONLINE);
});

1 Like

You should name the function, then you can easily use removeEventListener.

player.addEventListener(Twitch.Player.ONLINE, handleOnline);
function handleOnline() {
    // do something
    player.removeEventListener(Twitch.Player.ONLINE, handleOnline);
}

@3ventic thanks so much! For some reason I was hesitant to use it like that because I thought the removeEventListener wouldn’t know ‘which’ player without me using ‘this’ keyword.

Thanks!

It requires the function to remove as a parameter as you can register many handlers for the same event.

Thanks for sharing!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.