我已经在我需要的循环中创建了继承的 ajax 调用,如果 async 设置为“true”来克服这个问题,它会给出意外的输出我已经使调用同步,是否有任何方法可以等待继承的调用完成然后继续下一次迭代。我附上代码片段,如下所示。
在下面的代码中,我使用了 2 个同步调用,我需要将这些调用转换为异步,请建议我任何解决方案。
/// Main Loop
$.each(AllStagesvgdata, function (idx, svgdata) {
baseimageaspectratio = 1;
svgEditor.changeZoomManualFluid(0);
///process stages and controls
ParseStageControlsDynamic(svgdata.Id);
var filename = '';
var svginf = "<?xml version=\"1.0\"?>\n" + svgCanvas.getSvgString();
///process png image creation
ProcessSvgImageFiledynamic(svgdata.Id, filename, svginf);
});
///function to Create png files and html pages
function ProcessSvgImageFiledynamic(stageid, filename, svginf) {
var bbbox = "";
try {
var groupLayer_new = $("#svgcontent")[0]; //.find("#groupLayer_new")[0];
var bb = groupLayer_new.getBBox();
if (bb != null && bb.width != 0) {
bbbox = "" + bb.x + "," + bb.y + "," + bb.width + "," + bb.height;
}
}
catch (e) { }
$("#loadingnew").show();
$.ajax({
type: "post",
url: "/FrontEndPlan/PngFileSaveFluid", // change to full path of file on server
data: { "svgdata": svginf, "pngfilename": filename, "boundbox": bbbox },
dataType: "json",
cache: false,
async: true,
success: function (msg) {
$("#loadingnew").show();
printimgcount++;
if ($(AllStageImages).find("Plan > Stage[Id='" + stageid + "']").length > 0) {
$(AllStageImages).find("Plan > Stage[Id='" + stageid + "']").attr({ "ImageFileName": msg.filename, "ImageSrc": msg.fullfilename });
}
///check all the stages have been processed
if (AllStagesvgdata.length == printimgcount) {
///Create the html pages when all the svg data have been processed
CreateHtmlPagesForPrintPlan();
$("#loadingnew").hide(); }
},
error: function () { }
});
}
function ParseStageControlsDynamic(newtargetstageid) {
$.ajax({
type: "GET",
url: "/FrontEndPlan/GetResponseText",
data: { url: encodeURI(decodeURI(baseimage.attr("SRC").substr(0, 1) == "/" ? imgsrclocal + baseimage.attr("SRC") : baseimage.attr("SRC"))) + "?&ms=" + mytime },
dataType: "html",
async: false,
success: function (svgstringeval) {
svgCanvas.importSvgStringparentnode(svgstringeval, groupLayer_new, grouplayerdragged, 'baseimage_svg', '0');
$(AllStageXmlList).find("Plan > Stage[Id='" + newtargetstageid + "'] > Control").each(function () {
$("#loadingnew").show();
RuleImplemetationDynamic(newtargetstageid, $(this).attr("Title"));
});
},
error: function (data, status, e) {
alert(e);
}
});
}
///Function for rule implementation
function RuleImplemetationDynamic(stageid, controltile) {
$("#loadingnew").show();
$(AllStageXmlList).find("Plan > Stage[Id='" + stageid + "'] > Control[Title='" + controltile + "'] > ControlImage").each(function () {
if ($(this).attr("ImageInclude") == "true") {
AfterRuleImplementDynamic($(this).attr("ControlImageId"), stageid, controltile, true);
}
else {
AfterRuleImplementDynamic($(this).attr("ControlImageId"), stageid, controltile, false);
}
});
$("#loadingnew").hide();
}
///End of Function for rule implementation
//Function to add image after rule read to the svg canvas
function AfterRuleImplementDynamic(id, targetstage, targetcontrol, shouldinclude) {
var mytime = new Date().getTime();
$.ajax({
type: "GET",
async: false,
url: "/FrontEndPlan/GetResponseText",
data: { url: encodeURI(decodeURI(imgsrc)) + "?&ms=" + mytime },
dataType: "html",
success: function (svgstringeval) {
svgCanvas.importSvgStringparentnode(svgstringeval, groupLayer_new, grouplayerdragged, "layerimage_" + controlid, '1');
}
});
}