javascript - Array not accessed outside of function -


i'm trying use global arrays store data used @ later course.

however when run through chrome debugger console push function doesn't add them array associated either, nor errors.

i've looked @ many other examples , cannot see i'm going wrong.

could show me i'm going wrong?

javascript:

var hv = ['1', '2', '3', '4'];  $(document).ready(function () {     (var = 0; < hv.length; i++) {         getvmsonhyper(hv[i]);     } });  function getvmsonhyper(id) {     $.ajax({         type: "get",         url: "/api/function/" + id,         contenttype: "application/json; charset=utf-8",         datatype: "json",         success: virtualmachineinfo     }) }  var avrg_mem = []; var avrg_cpu = []; var avrg_lng = [];  function virtualmachineinfo(vmdata) {      var vmcount = vmdata.length;     avrg_lng.push(vmcount);      var mem_size = "medium";     var cpu_size = "medium";     (var = 0; < vmdata.length; i++) {         var vm_mem = vmdata[i].memory;          if (vm_mem > 6143) {             mem_size = "large";         }         else if (vm_mem < 2047) {             mem_size = "small";         }         avrg_mem.push(mem_size);          var vm_cpu = vmdata[i].cpus;          if (vm_cpu > 4) {             cpu_size = "large";         }         else if (vm_cpu < 2) {             cpu_size = "medium";         }         avrg_cpu.push(cpu_size);     } 

solution

not solution such building function check contents of array rather going on chrome debug console told me shows code working.

adding below @ end check:

function checkarrays() {     (i = 0; < avrg_mem.length; i++) {         var whatis = avrg_mem[i];     } 

your arrays updated correcty. can see in example (open console before), replace ajax function similar logic function: https://jsfiddle.net/gzfll0uv/

var hv = ['1', '2', '3', '4'];  $(document).ready(function () {     (var = 0; < hv.length; i++) {         getvmsonhyper(hv[i]);     } });  function getvmsonhyper(id) {      settimeout(function() {         virtualmachineinfo([{memory: id * 100, cpus: 111},                             {memory: id * 100, cpus: 222},                             {memory: id * 100, cpus: 333}]);     }, 1000); }  var avrg_mem = []; var avrg_cpu = []; var avrg_lng = [];  function virtualmachineinfo(vmdata) {      var vmcount = vmdata.length;     avrg_lng.push(vmcount);      var mem_size = "medium";     var cpu_size = "medium";     (var = 0; < vmdata.length; i++) {          //console.log(i);         var vm_mem = vmdata[i].memory;          if (vm_mem > 6143) {             mem_size = "large";         }         else if (vm_mem < 2047) {             mem_size = "small";         }         avrg_mem.push(mem_size);          var vm_cpu = vmdata[i].cpus;          if (vm_cpu > 4) {             cpu_size = "large";         }         else if (vm_cpu < 2) {             cpu_size = "medium";      }         avrg_cpu.push(cpu_size);     }      console.log(avrg_lng); } 

you can confused arrays empty if try check after virtualmachineinfo function in code. need in virtualmachineinfo function because code works that:

  1. init of functions , variables firstly
  2. register observe dom ready event
  3. handle event , iterate hv variable, call getvmsonhyper , register new event on ajax request resolved
  4. call virtualmachineinfo function asynchronously 4 times.

so, after last point arrays filled values.

you should use promises or defer in jquery lib catch moment when ajax requests resolved.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

android - Pass an Serializable object in AIDL -