user118939 user118939 - 3 months ago 11
jQuery Question

JavaScript & JQuery Labeling Issue

The below code gives me an "Uncaught SyntaxError: Undefined label" error when logged to the console.

I've done quite a bit research and I have a theory for the cause, but I can't find any documentation that will help me find a solution. In most cases, the error happens when someone does not put the JS label directly before the loop. In some cases, someone's label is getting invisibly glitched by things like JSCover putting in unexpected brackets.

Anyway, I think this is an issue with my JQuery $.getJSON function; but I don't know how to fix it.

Can anyone assist me by explaining to me why the getJSON is creating the label issue, and identity if there are any other problems? I'm only using a label because my loop wasn't stopping with a while loop; even if it was hitting my else statement. If you have a suggestion to alter the code, it would be much appreciated.

featuredLoop:
for(priorityActive = 0; priorityActive < streamerLength; priorityActive++){
var url = "https://api.twitch.tv/kraken/streams/" + streamer[i];
$.getJSON(url,function(data1){
if(data1.stream === null){
i++;
continue featuredLoop;
}else if(data1.stream != null){
activeStreamer = streamer[i];
streamerDetails();
break featuredLoop;
}
});
}

Answer

This should do it i believe.

var i = 0,
  streamer = [] //some array;

function getStreamer(i) {
  if (i < streamerLength) {
    var url = "https://api.twitch.tv/kraken/streams/" + streamer[i];
    $.getJSON(url, function(data1) {
      if (data1.stream === null) {
        i++;
        getStreamer(i);
      } else if (data1.stream != null) {
        activeStreamer = streamer[i];
        streamerDetails();
      }
    });
  } else {
    console.log("not found");
  }
}