在 $.each 函数中,我正在调用返回列表的 jquery ajax 方法,因此我想在其他函数中使用返回的列表,并且 jquery ajax 循环也将工作并连续返回值并调用其他函数,但是它没有并行发生,这意味着在完成 $.each 函数后,它正在使用最后返回的列表调用另一个函数。但是我希望一旦它的返回列表调用具有该返回值的另一个函数并像这样继续。
function Test() {
var x = $('#myHiddenVar').val();
$.each($.parseJSON(x), function (key, value) {
$.ajax({
type: 'POST',
url: '@Url.Action("GeoIPDashboardDataload", "Home")',
data: { branchId: value },
async: false,
error: function (xhr, status, error) {
console.log(error);
},
success: function (data) {
debugger;
if (data != null) {
$(data.logs).each(function (index) {
debugger;
var _id = index;
var _logId = data.logs[index].id;
var _dstIP = data.logs[index].dst_ip;
var _dstCountryName = data.logs[index].dst_geoData.country_name;
var _dstlatitude = data.logs[index].dst_geoData.latitude;
var _dstlongitude = data.logs[index].dst_geoData.longitude;
var _srcIP = data.logs[index].src_ip;
var _srcCountryName = data.logs[index].src_geoData.country_name;
var _srclatitude = data.logs[index].src_geoData.latitude;
var _srclongitude = data.logs[index].src_geoData.longitude;
var _threatType = data.logs[index].threat_type;
var locations = {
"id": _id,
"logId": _logId,
"dstIP": _dstIP,
"dstCountryName": _dstCountryName,
"dstlatitude": _dstlatitude,
"dstlongitude": _dstlongitude,
"srcIP": _srcIP,
"srcCountryName": _srcCountryName,
"srclatitude": _srclatitude,
"srclongitude": _srclongitude,
"threatType":_threatType
};
queryStr = { "locations": locations };
queryArr.push(queryStr);
});
companyName = data.comp;
branchName = data.branch;
attacks.init();
}
}
});
});
}
------------
attacks = {
interval: getRandomInt(attack_min, attack_max),
init: function () {
setTimeout(
jQuery.proxy(this.getData, this),
this.interval
);
},
getData: function () {
var srclat = arr[arrayCount].src_geoData.latitude;
var srclong = arr[arrayCount].src_geoData.longitude;
var dstlat = arr[arrayCount].dst_geoData.latitude;
var dstlong = arr[arrayCount].dst_geoData.longitude;
var sourceip = arr[arrayCount].src_ip;
var attackerIP = arr[arrayCount].dst_ip;
var srccountry = arr[arrayCount].src_geoData.country_name;
attackdiv_slatlong = arr[arrayCount].dst_geoData.country_name;
hits.push({
origin: { latitude: srclat, longitude: srclong },
destination: { latitude: dstlat, longitude: dstlong }
});
map.arc(hits, { strokeWidth: 2 });
boom.push({
radius: 7, latitude: dstlat, longitude: dstlong,
attk: attackerIP
});
map.bubbles(boom, {
popupTemplate: function (geo, data) {
return '<div class="hoverinfo">' + data.attk +'</div>';
}
});
arrayCount++;
this.interval = getRandomInt(attack_min, attack_max);
this.init();
},
};
因此,上述从 document.ready 函数调用的测试方法以及在将所有项目推入 queryArr 数组调用 attack.init 和在 attack.int 内部之后,我正在使用该数组列表。