我试图使用setTimeout函数在$ .ajax调用中嵌入$ .post但是当数据发送到我的回调函数时,我收到以下错误:
TypeError:data.sector未定义 for(var i = 0,len = data.sector.length; i< len; i ++){
任何帮助都是非常感谢分辨率。
ajax的东西:
var browserUTC = new Date().getTime();
var test = Math.floor(browserUTC / 1000);
//document.write(test);
$.ajax({
type : 'POST',
url : 'php_scripts/currentFile.php',
dataType : 'json',
data : { "func" : "getFirstAndNext", epochTime : browserUTC, "func" : "getUpdateAndInterval" },
success : function( data ) {
data1 = JSON.parse(data.first);
next = JSON.parse(data.next);
sector_callback( data1 );
console.log(data.interval);
console.log(data.update);
setTimeout(function () {
// ajax to retreive new file
$.post ('php_scripts/newFile.php',
{
epochTime : data.next,
dataType : 'json',
data : { "func" : "getNext", epochTime : data.next },
}),
removeSectors();
next = JSON.parse(data.next);
console.log('hello');
console.log(next);
sector_callback( next );
//console.log('worked');
console.log("Ding!",Date()); // This is where I will load first JSON file
setInterval(function () { console.log("Dong!",Date()); }, data.interval * 60 * 1000);
}, data.update);
},
error : function ( XMLHttpRequest, textStatus, errorThrown ) {
}
});
回调:
var allPolygons = [];
function sector_callback(data) {
//console.log(data);
var bounds = new google.maps.LatLngBounds();
for (var i = 0, len = data.sector.length; i < len; i++) {
coords = data.sector[i].geometry.coordinates[0];
siteNames = data.sector[i].properties.Name; // added for site names
health = data.sector[i].properties.Health;
calls = data.sector[i].properties.Calls;
drops = data.sector[i].properties.Drops;
blocks = data.sector[i].properties.Blocks;
TCCFs = data.sector[i].properties.TCCFs;
dcr = data.sector[i].properties.DCR;
TCCFpct = data.sector[i].properties.TCCFpct;
path = [];
for ( var j = 0, len2 = coords.length; j < len2; j++ ){ // pull out each set of coords and create a map object
var pt = new google.maps.LatLng(coords[j][1], coords[j][0])
bounds.extend(pt);
path.push(pt);
}
if ( health == 1 )
{
var polygons = new google.maps.Polygon({
path: path,
strokeColor: "#000000",
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map
});
} else {
var polygons = new google.maps.Polygon({
path: path,
strokeColor: "#000000",
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: "#000000",
fillOpacity: 0.35,
map: map
});
}
createClickablePoly(polygons, "Site: " + siteNames + "</br>" + "Total_calls " + calls + "</br>" + "Total_drops " + drops
+ "</br>" + "Total_blocks " + blocks + "</br>" + "Total_TCCF " + TCCFs + "</br>" + "DCR " + dcr + "</br>" + "TCCFR " + TCCFpct);
google.maps.event.addListener(polygons, 'mouseover', function() {
var currentPolygon = this;
currentPolygon.setOptions({
fillOpacity: 0.45,
fillColor: "#FF0000"
})
});
if ( health == 1 )
{
google.maps.event.addListener(polygons, 'mouseout', function() {
var currentPolygon = this;
currentPolygon.setOptions({
fillOpacity: 0.35,
fillColor: "#FF0000"
})
});
} else {
google.maps.event.addListener(polygons, 'mouseout', function() {
var currentPolygon = this;
currentPolygon.setOptions({
fillOpacity: 0.35,
fillColor: "#000000"
})
});
}
allPolygons.push(polygons);
}
}
答案 0 :(得分:0)
调用set_timeout时,调用$ .ajax的基础函数是否已经终止并因此删除变量'data'是否可能? $ .ajax()是一个异步函数......
例如:
function test() {
var bla = 'test';
$.ajax({
// config
success : function( data ) {
alert(bla); // should be 'undefined';
}
});
}
可能的解决方案:
next = JSON.parse(data.next);
指的是。因为,正如您对帖子的评论中所写的那样,我相信你的语法错误是2行以上。