I'm currently trying to use CanvasJS to render multiple charts on my page, and using AJAX to get the data for the tables. I'm using a loop on the AJAX to get the data values, and that's working fine atm, but I'm having problems on getting the value of a variable outside the AJAX.
This is the part I'm having problems with:
for(var i = 0; i < nMeses; i++) {
var mes = dtIni.addMonths(i);
var ano = mes.getFullYear();
mes = mes.getMonth()+1;
mes = (mes<10 ? '0' : '') + mes;
var res = ano + mes;
console.log(res);
$.ajax({
url: "getMesGrafico.php",
type: "GET",
dataType: "json",
data: {"m": mes},
success: function(data) {
console.log(res);
var chart = new CanvasJS.Chart("body"+res, {
animationEnabled: true,
theme: "light1", // "light1", "light2", "dark1", "dark2"
title:{
text: "Gráfico de contas a pagar do mês"
},
data: [{
type: "column", //change type to bar, line, area, pie, etc
dataPoints: data
}]
});
chart.render();
},
error: function(data) {
console.log("Error");
}
});
}
Inside the ajax>success I'm successfully getting the data I want, but I can't get the right value for 'res'. My console output is currently: 201912 202001 202001 202001
, but I'm trying to understand why the first and second values are different since they are in the same loop.