Hello on line 30 of my javascript I log an array to the console. on line 31 i try to access a specific item in that array and the result is always undefined. I don’t know how this is possible.
Please share your code, otherwise we are not going to be able to check it.
This is because $.getJSON is asynchronous. The for loop will complete before the data all comes back.
For example, do this:
console.log(isStreaming);
console.log(i);
//console.log(isStreaming[i]);
You will see that i
is always 8. isStreaming[8]
is undefined
.
For more information on this specific issue, see this:
I would greatly recommend you learn more about asynchronous JavaScript and callbacks. It is an essential “feature” (or as some call it “Callback Hell”) of Javascript.
Here is a great free tutorial:
http://thenetninja.co.uk/courses/asynchronous-javascript-tutorial
2 Likes