0

i create two function 1- for ajax call 2- for update info after X second

but i need to create (Variables) according by information in JSON and use it out side function .... in charts for example

function ajaxGetApi() {
 var data = null;
 var xhr = new XMLHttpRequest();
 xhr.withCredentials = true;
 xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
   var myJson = JSON.parse(xhr.responseText);
   //console.log(myJson + "" + myJson.user.length);

   //---------------------------------------


   //   var userName = myJson.user[i].userName;
   //   var cases_count = myJson.user[i].cases_count;

   console.log("--------------------------------");

   var status_id0 = myJson.status[0].status_id;
   var status_count0 = myJson.status[0].count;

   var status_id1 = myJson.status[1].status_id;
   var status_count1 = myJson.status[1].count;

   var status_id2 = myJson.status[2].status_id;
   var status_count2 = myJson.status[2].count;

   console.log(status_id0 + "-" + status_count0);
   console.log(status_id1 + "-" + status_count1);
   console.log(status_id2 + "-" + status_count2);

   console.log("--------------------------------");

   var gender_id = myJson.gender[i].gender_id;
   var gender_count = myJson.gender[i].count;

   console.log("--------------------------------");

   var prov_id = myJson.prov[i].province_id;
   var prov_count = myJson.prov[i].count;

   console.log("--------------------------------");

   var reg_id = myJson.reg[i].reg_id;
   var reg_count = myJson.reg[i].count;

   console.log("--------------------------------");


   //---------------------------------------

  }
 });
 xhr.open("GET", "data.php");
 xhr.send(data);
}

function updateInfo() {
 ajaxGetApi();
 setInterval(ajaxGetApi, 60000);
}

updateInfo();

thanx for help :)

  • It's not clear what you mean. Which code in your example isn't working as expected? – David Jun 01 '16 at 18:21
  • its work but when i use var prov_id for example outside function its not work ..... how i can use any var outside the function – Haitham Laith Jun 01 '16 at 18:25
  • In order to use something outside of the scope in which it's declared, you'd have to declare it outside of that scope. Declare `prov_id` outside of the function and other code will be able to access it. – David Jun 01 '16 at 18:26
  • sorry iam not follow you my function to get data from json and update how i use var outside this function can u show me ? – Haitham Laith Jun 01 '16 at 18:33
  • If you declare a variable inside a function, like this: `function something() { var someVariable = 1; }` then it will only be visible inside that function. But if you declare it *outside* the function, like this: `var someVariable = 0; function something() { someVariable = 1; }` then it will be visible *outside* the function. You can read up more about "scope" in JavaScript. – David Jun 01 '16 at 18:35
  • You need to learn about promises. Read http://blog.slaks.net/2015-01-04/async-method-patterns/ – SLaks Jun 01 '16 at 18:54

0 Answers0