好的,我有一個ajax調用recives一個json對象。jquery暫停循環,直到全局條件得到滿足
的whatn我試圖做的想法是得到舉報的所有記錄的列表,以及地理編碼他們做一個反向查找並保存到數據庫中,但作爲一個批次
我在想循環數據,然後執行額外的ajax調用(將對數據進行地理編碼),然後執行另一個ajax調用以保存到數據庫。
問題出在我的for循環中,一切運行得太快,調用和瀏覽器崩潰之間沒有停頓,或者保存到數據庫函數會添加錯誤的數據。
$('#geo_batch').click(function(){
var ajax_load = "<label><img src='/images/icons/loadinfo.gif' alt='saving location...' /> Loading data...</label>";
$("#batch_detail").html(ajax_load);
$('#batch_buttons').hide();
saveall = true;
var form = $("form"); //Grab the form element from the DOM
//alert(form.serialize());
var mydata = form.serialize();
$.ajax({
type: "POST",
url: 'geo_getupdate_list.php',
data: mydata,
dataType: 'json',
success: function(dat) {
processbatch(dat);// process the returned data
},
error: function(dat) { //Triggered if an error communicating with server
//alert('fail');
$("#batch_detail").html('<label>There was an error: '+dat+'<label>');
$('#batch_buttons').show();
}
});
return false; //Ignore the default behavior of the button click
});
過程數據功能
function processbatch(dat){
// Cache the batch_detail element
$("#batch_detail").html('<label>Locations have been retrieved:<br>' + dat + '<label>');
$('#batch_buttons').show();
var count = dat.location.length - 1;
for(i=0; i < count; i++){
$('#batch_detail').append('<li>address: ' + dat.location[i].geoaddr_mdt + 'flag: ' + dat.location[i].flag_mdt+'</li>');
$('#id_mdt').val(dat.location[i].id_mdt);
$('#entrytype').val(dat.location[i].idedt_mdt);
$('#name').val(dat.location[i].name_mdt);
$('#geo_addr').val(dat.location[i].geoaddr_mdt);
$('#telephone').val(dat.location[i].telephone_mdt);
$('#email').val(dat.location[i].email_mdt);
$('#geo_detail').val(dat.location[i].displayaddr_mdt);
$('#website').val(dat.location[i].website_mdt);
//$('#active').val(dat.location[i].active_mdt);
var address = dat.location[i].geoaddr_mdt;
// if address is not empty
if(address != '') {
address_lookup(address, region, 'update');
};
};
};
地址查找功能
function address_lookup(address, region, savetype) {
// set default region
if(region==null || region == '') {
region = 'uk';
};
// address not empty
if(address != '') {
//clear existing markers<br />
if(savetype == 'save'){
removemarkers();
};
$('#geo_detail').html('<label>Geocoding address...</label>');
// lookup the address
geocoder.geocode({'address':address,'region':region}, function(results, status) {
// if the address was found
if(status == google.maps.GeocoderStatus.OK) {
$str = '<label>Geocode Successful<br> Lattitude: '+results[0].geometry.location.lat()+' Longitude: '+results[0].geometry.location.lng()+'<br> The address is displayed below and will be stored in the database.<br> If the address is incorrect you may edit it before saving the location to the database.<br>If the marker is in the wrong location you may drag it to where you believe it should be.</label>';
$('#geo_detail').html($str);
// insert lat/long into form
$('#lat').val(results[0].geometry.location.lat());
$('#lng').val(results[0].geometry.location.lng());
// create new lat/long object
latlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
$('#disp_addr').val(address);
$('#form_buttons').show();
$('#detail_address').show();
//reverselookup(results[0].geometry.location.lat(), results[0].geometry.location.lng());
// set zoom option
map.setZoom(15);
// center the map on the new location
map.setCenter(results[0].geometry.location);
createMarker(map, latlng, true, false);
if(savetype ='update'){
savedata('update');
};
if(savedata='save'){
savedata('save');
};
} else {
// display error
$('#geo_detail').append('<label>Geocoder failed to retrieve address: '+status+'</label>');
$('#disp_addr').val($('#geo_addr').val());
};
});
};
};
編輯在回答Zacks的第一個評論----
我要顯示的每個結果作爲它正在被處理,然後將結果輸出到geo_detail div,所以有一個處理過的記錄和關聯的列表這意味着我希望讓用戶在處理每條記錄時檢查每條記錄,因此循環會暫停,直到用戶關閉保存按鈕,這可能會將全局變量設置爲true,這樣我的循環會檢查並等待,然後開始我需要某種暫停,以便在處理下一條記錄之前各種ajax調用有時間執行。
編輯完------------------------------ 任何意見或指針將不勝感激
感謝
你的目標是什麼?要一次顯示所有結果(例如每5秒一次),或顯示結果列表? – 2010-11-08 12:11:09
嗨zack,看到我的編輯在原來的問題... – 2010-11-08 12:29:11