Stuck on a bug: My code is constantly switching who is off and on

Stuck on a bug: My code is constantly switching who is off and on


Hey guys, i am stuck on a bug. So basically my code is confusing which twitch user is online and which is offline. How do i handle this?

If you want to see it for yourself just click few times on run.


You seem not to understand the asynchronous nature of XMLHttpRequest. You are using global variables (which is fine), but since the responses from API calls using XMLHttpRequest do not occur sequentially, your onOff variable when used in your code could have different values.

The following is a description of how you can salvage your current solution. It is not ideal, but it will work with the changes I describe. I recommend having your getLiveStatus function return a value of true or false and rewrite your functions to not use the global variable onOff, but that is up to you.

Band-aid Solution: You can add another parameter called onOff to both your channels function and your renderHTML function and you will need to pass the corresponding onOff value when calling these functions. Also, instead of:

for (var i=0;i<userBase.length;i++){

you will need to move the call to the channels function (with the changes I mentioned above) to be inside the streams function after you call getLiveStatus(dataOne). Instead of passing in userBase[i], you will just use user. This way, the correct user and corresponding onOff status will be passed at the same time to channels. Inside the channels function, you will need to call renderHTML with dataTwo and the onOff value, so it renders correctly.


Oh man, thank you. This was so helpful.