I am having an issue with my javascript code running on a web page.
I am having 1 chart using d3.js that is updating over time. the data are refreshed every 500ms to update the data of the chart using ajax. My problem is that, when the data getting fetch takes a bit too long, it is getting laggy as i need to set async to off if I want to get my array of data properly.
here is the code
document.addEventListener("DOMContentLoaded", function() {
timer = window.setInterval(updateData, interval);
....
});
function updateData() {
realTimeData=datafetch();
....
d3.select("#chart").datum(lineArr).call(chart);
}
function datafetch(LastTime) {
var rtn
$.ajax({
url: '/ajax/GetNewData',
data: {
'id': RecordID
},
async: false,
dataType: 'json',
success: function (data) {
if (data)
rtn=JSON.parse(data)
}
});
I know the code is a bit messy and I would listen to your comments for sure.
if I activate async, everything is smooth, but my variable realTimeData never gets the array and is always undefined. when async is on, it is a bit laggy everytime the data is fetch as all the js code is stopped waiting for the response from the server.
Does anyone have any hints on how to handle this problem? Or is there any root issue about the chosen technology?
Do you think I should refresh the chart first, then fetch the data, then after 500ms, update the chart again? it would make me loose 500ms or so in the data refreshing, so if there is another possibility, I would be happy to get some clues.
thank you,