-2

I have the following which creates an array (of sorts!):

var combined = [];
function slides(){
$.ajax({
  type: "GET",
  url: '../queries/get_all_slides.php',
  cache: false,
  dataType: "json",
  success: function(reply){
    $.each(reply.slides, function(a, b){
      combined.push({"src":b.slide_banner,"title":b.slide_title});
    });
  }
});
}

slides();

console.log(combined);

in the console I get:

Array []
​
0: Object { src: "1712681532_1920x1080.jpg", title: "Title 1" }
​
1: Object { src: "1713922664_1920x1080.jpg", title: "Title 2" }
​
length: 2

But when I try to get the values back, I get nothing:

function test(){
$.each(combined, function(x,y){
  console.log("y " + y);
  console.log("x " + x);
  $.each(x, function(a,b){
    console.log("hello 3");
    console.log("b " + b[0]);
  });
});
}
test();
2
  • Your slides function is an ansynchronous function. You should refer to this answer for a solution Commented Dec 4 at 13:48
  • 3
    The console results you're seeing may be out of sync and not reflect the reality at that moment in the logic. Any operation you want to perform on the data fetched asynchronmously (e.g. via AJAX) would need to be performed or invoked by that success callback (at least in this AJAX operation structure, there are other structures as well). Commented Dec 4 at 13:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.