我有JavaScript/jQuery代碼,它獲取信息並使用while/for循環的混合將其更新到數據庫中。在提取時,我有一個div,顯示正在進行的最新進度日誌。在Firefox中,隨着腳本運行,它會在它應該同時更新div。在谷歌瀏覽器中,它運行整個循環,保留日誌,並只輸出它,直到腳本完成運行。任何人都知道爲什麼會發生這種情況?爲什麼Javascript輸出在Google Chrome中被阻止?
這裏是我的代碼:
$(document).ready(function() {
add_text("test");
var array_length = num_sets;
for(var i = 0; i < array_length; i = i + 1) {
var setId = sets[i]['id'];
var setName = sets[i]['name'];
var setLinkName = sets[i]['link'];
var setNumCards = sets[i]['num_cards'];
add_text("Beginning to fetch set \"" + setName + "\"");
add_text("Found " + setNumCards + " total cards.");
while(ii < setNumCards) {
var card_name = sets[i]['cards'][ii]['name'];
var card_link = sets[i]['cards'][ii]['link'];
add_text("Fetching card " + sets[i]['cards'][ii]['name']);
fetch_card(sets[i]['cards'][ii]['link'], setId);
}
}
});
add_text功能:
function add_text(text) {
$("#status_text").append("<br />" + text);
}
fetch_card功能:
function fetch_card(card_link, set_id)
{
$.ajax({
url: "fetch_card.php?link=" + card_link + "&set_id=" + set_id,
context: document.body,
async: false,
success: function(){
ii = ii + 1;
}
});
}
什麼是'add_text()'的代碼? – LordZardeck 2012-02-19 00:03:47
function add_text(text){ $(「#status_text」)。append(「
」+ text); } – user1218595 2012-02-19 00:08:51
如果代碼是同步的(看起來像),通常瀏覽器將不會重繪,直到當前的同步代碼完成。如果它是同步的,我很驚訝Firefox在完成之前重新繪製。 – 2012-02-19 00:10:27