我尝试了几种方法来显示我的加载 gif,然后在 ajax 脚本加载完成时隐藏。我尝试了 $j(window).load 并且目前正在尝试将 hide() 函数放置在不同位置的加载 gif div 元素上。在函数中的其他脚本加载之前,我无法弄清楚如何显示加载 gif。你能看到我能做什么吗? (如果我注释掉所有 $j('#loading').hide() 我确实得到了加载 gif 显示)。 SimpleWithAttrPrices 函数执行 ajax 脚本并返回值。
function updatePrices(IDs){
var product_id= <?=$product_id ?>;
var qty= parseInt($j("#qtyUpdateBox input").val());
$j('#loading').show();
if (qty==1){
function sendRequest(i,basePrice) {
var optionSelectionArray = currentlySelectedAttributes(IDs[i]);
simpleWithAttrPrice(optionSelectionArray, function(data) {
//var vendor = IDs[i];
var basePrice = parseFloat(roundDollar(data));
$j('.details'+IDs[i]+ ' .priceBlock').empty();
$j('.details'+IDs[i]+ ' .priceBlock').append('<span>'+formatCurrency(basePrice,"$")+'</span>');
$j('.details'+IDs[i]+ ' .priceBlock').append('<input type="hidden" name="customPrice" value="' + basePrice + '"/>');
});
}//end sendRequest
for(i=0; i<IDs.length; i++)
{
sendRequest(i);
if(i=IDs.length-1){
$j('#loading').hide();
}
}
//$j('#loading').hide();
}//end if
//... THE REST OF THE FUNCTION NOT APPLICABLE
最佳答案
您可以使用ajaxStart()和 ajaxStop()在 ajax 请求期间显示/隐藏加载动画。
就这么简单:
$(document).ajaxStart(function() {
$j('#loading').show();
})
.ajaxStop(function() {
$j('#loading').hide();
});
关于javascript - 加载 gif 以在所有 ajax 脚本执行完毕后隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17035977/