我試圖找出一種方式來運行進度加載器,而谷歌地圖進程和呈現一個kml。谷歌地圖功能之前聽衆
我有一個監聽設置status_changed。
google.maps.event.addListener(kmllayer, "status_changed", function() {
var kmlstatus = kmllayer.getStatus();
if (kmlstatus != 'OK') {
$('kmllayerlist').append($('<div class="kmllayeritem">Error Loading KML' + kmlstatus +'</div>'));
} else {
var itemhostname = kmllayerURL.split("/");
$('#kmllayerlist').append($('<div class="kmllayeritem"><input type="checkbox" class="kmllayerchx" CHECKED><img src="http://www.google.com/s2/favicons?domain='+itemhostname[2]+'" class="kmllisticon" alt="kmlurlicon">'+itemhostname[(itemhostname.length - 1)]+'</input></div>'));
kmllayerarr.push(kmllayerURL);
}
});
如何讓一個函數運行直到偵聽器被觸發?我在想像下面這樣的東西,但我想不出一個好的條件來打破這個時間表。
while() {
//function to run until listener fires
}
工作代碼塊
function addkmlLayer_m(kmllayerURL) {
kmllayer = new google.maps.KmlLayer(kmllayerURL, {
preserveViewport: true
});
kmllayer.setMap(map);
var itemhostname = kmllayerURL.split("/");
var kmlstatus_boo;
function kmlProgress() {
if (kmlstatus_boo != 'done') {
google.maps.event.addListener(kmllayer, "status_changed", function() {
kmlstatus_boo = 'done';
$('#progress').remove();
kmlstatus = kmllayer.getStatus();
if (kmlstatus != 'OK') {
$('#kmllayerlist').append($('<div id="kmllayeritem_error" class="kmllayeritem">Error Loading KML ' + kmlstatus +'<input class="kmldel" type="image" src="../images/delete.png" name="delete kml" OnClick="delkmlerr();" /></div>'));
} else {
kmllayerarr.push(kmllayer);
$('#kmllayerlist').append($('<div id="kmllayeritem_'+kmllayerarr.length+'" class="kmllayeritem"><input type="checkbox" class="kmllayerchx" CHECKED><img src="http://www.google.com/s2/favicons?domain='+itemhostname[2]+'" class="kmllisticon" alt="kmlurlicon">'+itemhostname[(itemhostname.length - 1)]+'</input><input class="kmldel" type="image" src="../images/delete.png" name="delete kml" OnClick="delkmlitem('+kmllayerarr.length+');" /></div>'));
}
});
$('#kmllayerlist').append($('<div/>', {'id':'progress','class':'kmllayeritem','text':'Loading '+itemhostname[(itemhostname.length - 1)]+' KML'}));
kmlstatus_boo = null; // free the closure
} else {
window.setInterval(kmlProgress,400); // Run again in 400ms changed so the user actually sees the loading image.
}
}
kmlProgress();
}
我覺得這個方法有潛力。您是否建議我將偵聽器放入myTimer函數中,並且偵聽器未被觸發(myRelevantValue =「False」),然後使用else運行progrress加載器?我會試試這個。 – normalhabit
我用這種方法和@ sheikh-heera說的一些創建解決方案。關鍵是將status_change監聽程序放入timerfunction中。 – normalhabit
偵聽器將在myTimer函數之外。它只需要設置一次,並且只要有事情發生變化就會多次觸發。如果你把監聽器放在裏面,那麼每當定時器觸發時你都會增加一個監聽器,這是你不想要的。我沒有在谷歌地圖api中使用kml,所以我不確定那裏的細節,但它應該是微不足道的,以便觀察你需要它觀察的變量。請記住在完成清除閉包時將它們設置爲空。 –