I have a api call which I am calling every 5 seconds. After each interval I want to store especially Marks
from the api call into an js object/array so that I can pass that to a function to draw a graph. I have declared a var marks = []
and tried to assign data from api to marks, but when I am doing console.log(marks);
outside the interval loop it says undefined.
The json data looks like this
{
"PersonIds": [
"001",
"002"
],
"Subjects": [
{
"Name": "Maths",
"Marks": 90
},
{
"Name": "Science",
"Marks": 70
},
]
}
$(document).ready(function (){
//Start the graph
marksGraph();
var marks = []; //Contains marks
var names = [];
var rg, // Storesstartdate and end date range
startDt, // Stores start Date
endDt; // Stores end Date
//Calling the Range API
d3.json('url', function(error,data) {
if(rg != null || rg!= undefined || !error){
rg = data;
startDt = new Date(data.startDt);
endDt = new Date(data.endDt);
//Set Count to zero
var count = 0;
var interval = setInterval(function process(){
if (startDt > fromDt) {
clearInterval(interval);
return
}
console.log("I am coming inside Interval");
var startDate = startDt.toISOString();
var endDate = new Date(startDt.setMinutes(startDt.getMinutes() + 10)).toISOString();
//Calling the API
d3.json("url", function(error,datum) {
if(datum != null){
names = datum.name;
marks = datum.marks;
}
});
count++;
},2500);
console.log(marks);
}
});
console.log(names);
console.log(marks);