I'm sure this is simple but I'm not really a JS developer.
I've got the following function that sets two arrays equal to a json response so that I can load the arrays into Chart.js
$(document).on("turbolinks:load",function(){
var months = [];
var values = [];
$.getJSON({url: "/reports/graph_data.json", success: function(data){
for (var i = 0, len = data.length; i < len; i++) {
months.push(data[i][0]);
values.push(data[i][1]);
}
alert(months); //DISPLAYS CORRECT MONTHS
}});
alert(months); //DISPLAYS NOTHING
});
Notice that I have two alerts in the code. The first alert displays the months from the response as I would expect but the second alert is empty. Why is this? I would've thought the scope wouldn't be broken when I referenced the array outside of the getJSON function.
Thanks in advance.