0
Google Maps API v3の関数を記述しようとしています。基本的には、ある形状のすべてのポリゴンポイントをループして、各ポイントをPHP関数に渡してデータをMySQLに挿入します。複数の非同期関数を実行して結果を待っています
function saveSite() {
// Create an array to hold the individual point saving results
var submission_Result = [];
var final_result = true;
// Get the general properties for this shape
var shape_ID = selectedShape.ID;
var shape_Name = document.getElementById("info_name").value;
var shape_Description = document.getElementById("info_description").value;
var shape_Type = shapesArray[shape_ID].type;
var shape_Points = selectedShape.getPath();
// Run through all points in the shape and save them
for (var i = 0; i < shape_Points.length; i++) {
var curPoint = shape_Points.getAt(i);
// Prepare the point data to be saved
var url = pageDir + "phpsqlinfo_addShapeData.php?db=" + database + "&job=" + jobnumber +
"&stype=" + shape_Type + "&snumber=" + shape_ID + "&sname=" + encodeURIComponent(shape_Name) +
"&sdesc=" + encodeURIComponent(shape_Description) + "&sseqno=" + i + "&slat=" + curPoint.lat() + "&slng=" + curPoint.lng();
// Attempt to save the shape data
downloadUrl(url, function(data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
submission_Result.push(true);
} else {
submission_Result.push(false);
}
});
}
// Run through all point results and confirm correct submission of points
for (var i = 0; i < submission_Result.length; i++) {
if (submission_Result[i] == false) {
final_result = false;
}
}
// If all points were successfully saved, tell the user
if (final_result == true) {
// All points saved successfully so do something
} else {
// Something went wrong and not all points were added so do something else
}
}
私は* downloadURL *非同期関数として実行されていることを知っているので、私は* submission_result *配列を通して、現在のループができず、すべてのポイントが正しく追加されたかどうかを確認してください。 私の質問は、すべての非同期呼び出しが完了し、結果配列をチェックするのを待つことができる方法はありますか?私は完全に困惑してたぶん例えば、このような何か?:
});
}
while (submission_Result.length < shape_Points.length) {
// Do something wait a short while
};
// Run through all point results and confirm correct submission of points
for (var i = 0; i < submission_Result.length; i++) {
任意のアイデアをいただければ幸いです
!
ありがとうございます。
まさに私が必要としたものであり、それは魅力のように機能します。非同期のjavascriptはまだ私にはかなり新しいので、まだこれらの欠点に慣れています。 – JohnHenry