# Get current status of Player in event listener attached to it?

**URL:** <https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787>\
**Category:** API\
**Created:** [July 4, 2016, 2:37pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787 "2016-07-04T14:37:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Argus9](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/argus9/32/1886_2.png) [@Argus9](https://discuss.dev.twitch.com/u/Argus9)\
**Post date:** [July 4, 2016, 2:37pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/1 "2016-07-04T14:37:50Z")

</div>

Hey folks,  
I’m making a web app that embeds multiple Twitch players in a single page, and the app checks the channel’s online/offline status to decide how to order them in the page (by changing channels using `setChannel(...)`. I’m trying to use event listeners to get the best responsiveness on this (compared to polling the `channels` API, which can lag behind by minutes at times), but a problem arises when I’m trying to define the Players’ ‘online’ and ‘ended’ event listeners (I chose ‘ended’ over ‘offline’ because ‘offline’ seems to get called multiple times, while ‘ended’ is only called once, when a stream goes offline); I can pass in the Player object I’ve defined, but the event listener will always reference the _initial_ state of the Player rather than its most up-to-date state. See code sample below:

```
player = new Twitch.Player( id, { channel: 'kinggothalion' } );
player.addEventListener( 'online', recalibratePlayers( player.getChannel(), 'online' ) );
player.addEventListener( 'ended', recalibratePlayers( player.getChannel(), 'offline' ) );

```

Is there a way I can instead have these event listeners poll the current state of the Player object they’re assigned to to get the current channel of the player? Thanks!

---

<div class="post-metadata">

**Author:** ![DallasNChains](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/dallasnchains/32/1608_2.png) [@DallasNChains](https://discuss.dev.twitch.com/u/DallasNChains)\
**Post date:** [July 5, 2016, 3:49pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/2 "2016-07-05T15:49:59Z")

</div>

Just to make sure I’m understanding, you initialize the player with channel kinggothalion, setChannel(‘otherchannel’), and then getChannel() still returns kinggothalion?

---

<div class="post-metadata">

**Author:** ![Argus9](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/argus9/32/1886_2.png) [@Argus9](https://discuss.dev.twitch.com/u/Argus9)\
**Post date:** [July 5, 2016, 10:43pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/3 "2016-07-05T22:43:08Z")

</div>

@DallasNChains, not exactly. When I add the event listener to the Player, it’s calling `player.getChannel()`, which gets the currently-assigned channel when I define the event listener. So future event callbacks calling the original channel don’t surprise me.

I also tried passing the `player` object instead of `player.getChannel()`, but this also only returns the _current state_ of the player, so I get the same result if I try to invoke `player.getChannel()` in the `recalibratePlayers` method.

What I need is some way to access the current state of the player that an event listener is attached to when it’s called.

---

<div class="post-metadata">

**Author:** ![DallasNChains](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/dallasnchains/32/1608_2.png) [@DallasNChains](https://discuss.dev.twitch.com/u/DallasNChains)\
**Post date:** [July 7, 2016, 2:34am UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/4 "2016-07-07T02:34:02Z")

</div>

Oh! I see. You’re looking for the object that fired the event listener. You’ll need to research `this` in JavaScript. In particular, I would look at the section titled [“The value of this within the handler” on MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener).

---

<div class="post-metadata">

**Author:** ![JB940](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@JB940](https://discuss.dev.twitch.com/u/JB940)\
**Post date:** [July 7, 2016, 12:56pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/5 "2016-07-07T12:56:05Z")

</div>

I’ve noticed you don’t bind an actual function.  
You need to wrap your recalibratePlayers functions into an anonymous function, or create a synonym.

Either replace both  
`recalibratePlayers( ...)`  
with  
`function() { recalibratePlayers( ...); }`  
or replace them both with:  
`recalibratePlayer.bind(player,arg1,arg2)`

What the code you gave is doing, is binding the ‘RESULT’ of the function recalibratePlayers as an event listener. IE, it executes the function in place ONCE. It executes the function and puts the return value or whatever it does in the addEventListener function.

read more about [function binds here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)

---

<div class="post-metadata">

**Author:** ![Argus9](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/argus9/32/1886_2.png) [@Argus9](https://discuss.dev.twitch.com/u/Argus9)\
**Post date:** [July 8, 2016, 5:18pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/6 "2016-07-08T17:18:07Z")

</div>

@DallasNChains, @JB940, thank you for your replies! I’ll give those a try and see if I get the correct results.

---

<div class="post-metadata">

**Author:** ![Argus9](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/argus9/32/1886_2.png) [@Argus9](https://discuss.dev.twitch.com/u/Argus9)\
**Post date:** [July 8, 2016, 11:30pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/7 "2016-07-08T23:30:59Z")

</div>

@JB940’s suggestion worked for me. Putting the call to `recalibratePlayers(player.getChannel(), ...)` inside an anonymous function when adding the event listener passes the current status of the player. Thanks for all your help!

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [August 7, 2016, 11:31pm UTC](https://discuss.dev.twitch.com/t/get-current-status-of-player-in-event-listener-attached-to-it/6787/8 "2016-08-07T23:31:01Z")

</div>

Closed on August 7, 2016, 11:31pm UTC

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