Pulling my hair out here.
Jquery, Multiple JSON files & Ajax
I have a python script that is gathering data and dumping JSON files into a folder on my webserver
I am trying to visualise that data.
Basically I am first drawing an SVG map and coloring it in with a dummy json file. Subsequently I want to color it in using the JSON data. Each JSON file will represent one complete rendering (coloring) of the map.
I am using an Ajax call to a php script that returns the files in the directory. I then want to use Ajax (or the shorthand .getJson) to lad the data in that file -colour the map, and then move on to the next one (end result is an animation). The problem is the asynchronous nature of AJAX and not having any control over the timely execution and completion of the Ajax bit. Obviously i don't want to make a synchronous call because i don't want to lock up the browser.
Here's my code (apologies -it's fairly hefty)
jQuery(document).ready(function() {
$(function() {
var map, c = [];
var dep_data;
var val = {};
var max= 0;
var vals = new Array();
c = $('#map');
c.height(c.width()*.5);
drawMap('mapData.json');
function drawMap(url){
console.log(url);
$.ajax({
url: 'mapData.json',
dataType: 'json',
success: function(data) {
dep_data = data;
map = window.m = $K.map('#map', 600, 800);
map.loadMap('ireland.svg', function() {
map.loadStyles('./mapping_files/style.css');
map.addLayer({
id: 'regions',
key: 'name-1'
});
colourMap(dep_data);
var mapData = $.ajax({
url: './php/getfiles.php',
type : 'POST',
dataType: 'json',
success: function (files){
for (_a in files.response){
for (_b in files.response[_a]){
$.ajax({
url: files.response[_a][_b],
dataType: 'json',
success: function (json){
colourMap(json);
$(this).dequeue();
}
});
}
}
},
error: function (files){
console.log(files.message);
},
});
});
}
});
}
colourMap = function(data) {
//do the coloring in...
}
}); });