-2

I can't access the global variable which stores some ajax response data. When I console log the object itself, it gives me all the details, but when I console log the arrays in that object, it returns me []. I have set the ajax request to be synchronous.

//let define some global valibables first
var urlun = "#";
var resultObject = {};
resultObject.logo = [];
resultObject.url = [];
resultObject.namee = [];
resultObject.statuss = [];
resultObject.append = appendContent;

var channelArr = ["freecodecamp", "storbeck", "sheevergaming", "habathcx", "RobotCaleb", "ESL_SC2", "ogamingsc2", "noobs2ninjas", "beohoff", "askjoshy", "ggnetworktv"];

//this function is to get all channel's info, those that are not streaming
function getAllnotStreaming(data) {
  var thisItemIsAppend = false;
  for (var i = 0; i < resultObject.namee.length; i++) {
    if (resultObject.namee[i] == data.display_name) {
      thisItemIsAppend = true;
    };
  } // end for
  //real thing
  if (thisItemIsAppend == false) {
    resultObject.logo.push(data.logo);
    resultObject.url.push(data.url);
    resultObject.namee.push(data.display_name);
    resultObject.statuss.push("offline");
  }
}

//get all streaming channle's info
function getAllnowStreaming(data) {
  if (data.stream) {
    resultObject.logo.push(data.stream.channel.logo);
    resultObject.url.push(data.stream.channel.url);
    resultObject.namee.push(data.stream.channel.display_name);
    resultObject.statuss.push(data.stream.channel.status);
  }
}

//to append channel to page, wheather it's streaming or not, all you need a one function
function appendContent(logo, url, name, status) {
  if (logo == null) {
    logo = "https://dummyimage.com/50x50/ecf0e7/5c5457.jpg&text=0x3F";
  };
  var statushtml = '<div class="row channels onlinenow"> <div class="col-md-2"><img class="logoimg" src="' + logo + '"></div> <div class="col-md-4"><div class="name"><a target="_blank" href="' + url + '">' + name + '</a></div></div> <div class="col-md-6"><div class="streaming">' + status + '</div></div></div>';
  $(statushtml).appendTo("#content");
}

//below are control buttons
function showOnlineOnly() {
  $(".offlinenow").show();
  $(".onlinenow").show();
  $(".offlinenow").hide();
}

function showOfflineOnly() {
  $(".offlinenow").show();
  $(".onlinenow").show();
  $(".onlinenow").hide();
}

function showAll() {
  $(".offlinenow").show();
  $(".onlinenow").show();
}

$(document).ready(function() {
  //regestor contol event
  $("#showOnlineOnly").click(showOnlineOnly);
  $("#showOfflineOnly").click(showOfflineOnly);
  $("#showAll").click(showAll);

  // get all now streaming channel first
  for (var i = 0; i < channelArr.length; i++) {
    var url = "https://api.twitch.tv/kraken/streams/" + channelArr[i] + "?callback=?";
    $.ajax({
      dataType: "json",
      async: false,
      url: url,
      success: getAllnowStreaming,
    });
  } //end of for loop

  // let's now get not steaming channels
  for (var i = 0; i < channelArr.length; i++) {
    var url = "https://api.twitch.tv/kraken/channels/" + channelArr[i] + "?callback=?";
    $.ajax({
      dataType: "json",
      async: false,
      url: url,
      success: getAllnotStreaming,
    });
  } //end of another for loop

  //let's append content
  console.log(resultObject);

  console.log(resultObject.logo);
  for (var ii = 0; ii < channelArr.length; ii++) {
    resultObject.append(resultObject.logo[ii], resultObject.url[ii], resultObject.namee[ii], resultObject.statuss[ii]);
  }

});
Andy
  • 61,948
  • 13
  • 68
  • 95
Stanley Lau
  • 21
  • 1
  • 3
  • You should turn on `async` and learn how to use promises. – Andy Apr 21 '16 at 07:51
  • Do u have all this code inside the success handler.Can u share the entire ajax related code – Vijaykrish93 Apr 21 '16 at 07:51
  • This is not about async calls as the OP is using AJAX _synchronously_. – Andy Apr 21 '16 at 07:52
  • @Andy Who can tell from the sample given? We at least require the relevant parts *here*. – deceze Apr 21 '16 at 07:53
  • Yeah, I know - I did look at the codepen to see what was happening tho. Close the question for any reason other than it being a dupe of that question you linked to because that was untrue @deceze – Andy Apr 21 '16 at 07:55
  • hi, thanks you guys, the issue is now fixed using the method Oksid describe, i'm sorry if my posting is a bit unprofessional, I'm still learning hwo to use stackover – Stanley Lau Apr 21 '16 at 09:02

1 Answers1

-1

do you wait for the ajax calls to finish? I would say that at that time the object is empty. You should probably do the append in the getAllnowStreaming and getAllnotStreaming.

Oksid
  • 60
  • 3