TwitchTV project issues

Hey guys, I have tried seemingly everything to diagnose why my code keeps returning “undefined”. It is probably something real simple but I’m at the end of my rope. Here is the project: https://codepen.io/escottalexander/pen/JEKKpa?editors=0010

Thanks for looking into it.

possibly return only
Active
Inactive

not the html code <span … blah, blah blah …

Thats a good idea but I am also changing the button color with my code. I don’t understand why it won’t work.

I think I know whats wrong with this code. Because the getJSON part is asynchronous the rest of the code performs before it gets a response. I’d appreciate any ideas as to how I might make the rest of the code wait for the response. Maybe a callback?

EDIT

No, I was wrong. I tried to get this to work and it didn’t solve any problems.

try here -

I haven’t figured anything out on this problem. I don’t understand why it keeps on returning “undefined”. I have changed what it is returning and I have tested to make sure it is actually executing that part of the code but I can’t understand why it returns “undefined”.

I looked at your code a while, and couldn’t quite sort out the problem, but I can tell you what I found.

your isActive function works basically properly as far as I can tell. It does properly define isActive, but it fails to pass that to pullJSON, where labelActivity is undefined.

function pullJSON(streamName) runs 8 times before function isActiveTest(streamName2) once.

Which made me check the following:

if you put “return ‘cats’” right at the start of isActiveTest, it labels each of the streams status as “cats” – everything works properly. (if you wanted everyone to be cats, which I guess you don’t)

If you put “return ‘cats’” after the $document.return(function(){ … }) labelActivity becomes undefined in pullJSON again…

I should get to sleep, but this is what I would do next: change your code so that isActiveTest runs and stores a value (in an array or something?) for each of the streams before you do the pullJSON function. The problem seems to be that the line

labelActivity = isActiveTest(streamname)

is not waiting for the function to finish. This is why all the console logs run AFTER pullJSON has run all 8 times. (If you do a console log before the getJSON in isActiveTest, you see that the console logs in pullJSON and isActiveTest alternate) So you need to get those values first, and then do pullJSON and append that. Sorry, I’m tired, so this isn’t making much sense. But that really should fix the problem.

Thank @gtrabbit that makes perfect sense. I found out while talking with some people in the chat room that when you use getJSON then the return request sends itself to where it got the getJSON. That is why it keeps returning “undefined”. It doesn’t have anything to return to my code because the return line is going out to the api server. Your deduction is great and you might have a solution to work around the problem. Thanks!

1 Like

That is very good to know. Glad I could be of some help. Hopefully you’ve got it sorted out now.

I reorganized the structure and stopped relying on the return line and now my project is complete! Thanks for your help! https://codepen.io/escottalexander/pen/JEKKpa