0

I have created a global array (I know it is not recommended) that I would populate dynamically with data from an AJAX request. It looks like this:

var myArray = [];

$(document).ready(function() {
  var min = "someUrl.js";

  function somenewFunc(json) {
    alert(json);
  }

  (function someFunc() {
    $.ajax({
      type: 'GET',
      url: min + '?callback=jsonp',
      crossDomain: true,
      dataType: 'jsonp',
      jsonpCallback: 'somenewFunc',
      jsonp: 'callback',
      data: {},
      success: function(data) {
        myArray = [];

        $.each(data, function(i, item) {
          myArray.push(item.id);
        });
      },
      complete: function() {
        setTimeout(someFunc, 15000);
      }
    });
  })();
});

console.log(myArray);

I am deleting the array's content at the beginning of the success method as I don't want to it hold the same data multiple times. I could display the array's content inside the AJAX request but not outside. The console output shows me an empty array.

Any ideas what I am doing wrong? Thanks in advance!

webbul
  • 162
  • 1
  • 2
  • 12

0 Answers0