我使用函數showVisible()從googlemap收集所有標記ID,將它們傳遞給數組,然後使用AJAX從服務器調用相應的對象數據。返回false,不取消每個循環
只要有人在地圖上點擊標記集羣,就會觸發此操作。如果在所有ajax調用完成之前有人點擊標記,我需要停止循環遍歷數組。
我試圖使用if語句,以便如果breakAjax是「true」,那麼循環應該返回false。即使我可以在控制檯上看到變量變爲true,但它不會觸發中斷。
該函數的全碼是如下:
function showVisible() {
visRunning = true
console.log('show visible')
$('#objects_list').empty()
function unique(list) {
var result = [];
$.each(list, function(i, e) {
if ($.inArray(e, result) == -1) result.push(e);
});
return result;
}
scoots = unique(locStr)
totalScoots = scoots.length
//console.log('total scoots ' + totalScoots);
var scootOutput = 0
for (var i = 0; i < scoots.length; i++) {
if (scootOutput === 0) {
$('#objects_list').empty()
}
console.log('breakAjax: ' + breakAjax)
countManager()
$.ajax({
url: 'https://scootapi.stuffstory.com/api/stuff/' + scoots[i],
dataType: 'json',
type: "get",
success: function(stuffData, textStatus, jqXHR) {
$('#objects_list').append('<a class="stuff_wrapper" target="_blank" onclick="ga("send", "event", "grid view scooter ' + stuffData.id + '", "click", "frontPage")" href="../scooter/?view=' + stuffData.id + '"><div class="stuff_holder" style="background-image:url(' + stuffData.image + '/convert?w=250&h=250);"><div class="scooter_title_sm"><div class="title_wrapper"><div class="scooter_title_1 cursive_sm">' + stuffData.make + '</div><div class="scooter_title_2 lato_sm">' + stuffData.model + '</div><div class="scooter_title_3 cursive_sm">' + stuffData.year + '</div></div></div></div></a>')
console.log('breakAjax: ' + breakAjax)
stuffSizer()
scootOutput++
if (scootOutput === totalScoots) {
visRunning = false
}
},
error: function(data, textStatus, jqXHR) {}
})
if (breakAjax === "true") {
return false;
}
};
}
'breakAjax'是一個布爾值嗎?如果是這樣,if(breakAjax ===「true」)'if(breakAjax === true)'? – boombox
糟糕。這是最初的,然後我在這裏發佈之前只是爲了測試而改變它。我會改回來的,但似乎沒有任何區別。 –
我沒有看到任何代碼給breakAjax賦值,所以它怎麼會是真的或「真」? – Quentin