0

我正在从 Ajax 函数中的 XML 文件中读取图像的 URL,其中异步设置为 false,因此它应该等待 Ajax 函数完成。在接收这些 URL 时,我正在调用另一个函数来验证 URL 是否具有有效图像,并且我从回调函数“isUrlIsImage()”接收布尔值......但是我发现 Ajax 不会等待从回调函数接收所有结果...谁知道在哪里看???我怎样才能强制 Ajax 等待所有回调函数...非常感谢提前...

$.fn.initializeImg = function (CP_ID) {

var selected_dataType = "";

if (currentBrowser().browser == "IE" && currentBrowser().version < 10) {

    selected_dataType = "text";
}
else {
    selected_dataType = "xml";
}

$.ajax({
    type: "GET",
    url: "XML_file.xml",
    dataType:  selected_dataType,
    async:false,
    success: function (xml) {

        var processedXML_01 = parseXml(xml);


      $(processedXML_01).find('property').each(function () {

      var j_propertyIn_List = $(this).find('propcode').text();

           if (j_propertyIn_List == gb_var.j_propcode) {

       $(this).find('photo').each(function (index) {

       gb_var.j_propertyImgURLs[index] = $(this).find('urlname');

     console.log(">>>>   "+gb_var.j_propertyImgURLs[index].text());

      isUrlIsImage(gb_var.j_propertyImgURLs[index].text(), function (_url, result) {

    //alert("check result >> "+result + " for " + _url);

     if (result) {

      console.log("******TRUE*********  check result >> " + result + " for " + _url);

     }
     else {
      console.log("******FALSE*********  check result >> " + result + " for " + _url);

                       }
                   });
               });
            }
        });
    },
    error: function () {
        alert("An error occurred while processing XML file.");
    }

  });

  } //end plugin function 

  })(jQuery);


function isUrlIsImage(given_url, callback) {

console.log(" checking............. " + given_url);

url = given_url;

   $("<img>", {
       src: url,
       error: function () { callback(url, false); },
       load: function () { callback(url, true); }
      });
  }
4

0 回答 0