I just got this function working this evening. However, after a few minor edits getting a different part of the if/else to go, part of it has magically stopped working.
The four for loops in the callback function of the json request all execute, but none of the DOM manipulations are actually made. I've triple checked that the appropriate DOM elements exist for this to happen. Alerts will fire in all of them. Just the jQuery lines are failing.
I have tried putting the relevant variables into the console and manually iterating through the numbers to simulate a loop. This works fine. I've also used alerts to display the sequence of the variables in the loop, and these are all functioning properly.
I'm baffled.
function drawPreview() {
var $preview = $('#preview');
$preview.remove();
$('#general_preview').remove();
try {
activecell.location;
}
catch (error) {
$('#active_cell').clone().attr('id','general_preview').appendTo('#win_preview');
return;
}
if (activecell.location.match(/^\d+_\d+$/)!==null) {
var x = parseInt(activecell.location.slice(0,activecell.location.indexOf("_")));
var y = parseInt(activecell.location.slice(activecell.location.indexOf("_")+1));
var area = "x"+activearea.join("x")+"x";
$('#win_preview').append($('<div id="preview"><div><div></div><div></div><div></div></div><div><div></div><div></div><div></div></div><div><div></div><div></div><div></div></div></div>'));
var i = y-1;
var j = x-1;
function loadCell() {
var exp = new RegExp("x"+i+"_"+j+"x","g");
if (area.match(exp)) {
if (i==y&&j==x) {
$('#active_cell').clone().children().unwrap().appendTo($preview.children().eq(1).children().eq(1));
++j;
loadCell();
}
else {
var jqxhr = $.getJSON('data/areas/'+$('#select_area').val()+'/'+x+"_"+y+'.json', function(data) {
var tmp = data;
for (var l=0; l<9; ++l) {
$preview.children().eq(i-y+1).children().eq(j-x+1).append('<div></div>');
}
for (var l=0; l<9; ++l) {
$preview.children().eq(i-y+1).children().eq(j-x+1).children().append('<div></div>');
}
for (var l = 0; l < 9; ++l) {
for (var m = 0; m < 9; ++m) {
$preview.children().eq(i-y+1).children().eq(j-x+1).children().eq(l).children().eq(m).attr("style","background: #"+tmp.p.c[tmp.c[l][m]-1]+" url(textures/terrain/"+tmp.p.t[tmp.t[l][m]-1]+".png) bottom center no-repeat");
}
}
if (i==y+1&&j==x+1) {
return;
}
else if (j==x+1) {
++i;
j = x-1;
loadCell();
}
else {
++j;
loadCell();
}
})
.error(function() { alert("There was an error loading the data. The data may be invalid or you may be looking for a file that does not exist."); })
}
}
else {
if (i==y+1&&j==x+1) {
return;
}
else if (j==x+1) {
++i;
j = x-1;
loadCell();
}
else {
++j;
loadCell();
}
}
}
loadCell();
}
}