所有 ajax 调用完成后显示加载屏幕
嘿, 我的 ajax 调用有问题。我试图显示一个加载屏幕,它基本上是一个 div,在发出 10 个 ajax 请求时显示和隐藏。它似乎在 firefox 3.6 中工作正常,但 div 在 chrome 10 和 IE8 中没有显示。问题是 div 正在显示,但它只显示了几毫秒,然后就被隐藏了,尽管它在 ajax 调用之前被打开了。这是功能:
function addAllToPlaylist() {
var title;
var i = 1;
var percentage = 0;
var total = $('.tdtrackname').size();
$('#loadingscreen').show();
$('.tdtrackname').each(function() {
$.ajax({
async: false,
url: 'ajax/addsongtoplaylist.php?query=' + $(this).html(),
success: function(data) {
$('#divajax').html(data);
percentage = Math.round((i / total) * 100);
$('#loadingmsg').html('<h3>Adding songs...please wait<br>' + i + ' / ' + total + ' (' + percentage + '%)</h3>');
}
});
i++;
});
$('#loadingscreen').hide();
}
我希望你能帮我解决这个问题,我不知道为什么加载屏幕打开这么晚。 提前致谢
Hey,
i have a problem with an ajax call. i'm trying to show a loading screen which is basically a div which shows and hides while 10 ajax requests are made. it seems to work fine in firefox 3.6 but the div isn't showing up in chrome 10 and IE8. the thing is the div is showing up but its only showing up for a couple of milliseconds before it gets hidden eventhough its being opened before the ajax call. here is the function:
function addAllToPlaylist() {
var title;
var i = 1;
var percentage = 0;
var total = $('.tdtrackname').size();
$('#loadingscreen').show();
$('.tdtrackname').each(function() {
$.ajax({
async: false,
url: 'ajax/addsongtoplaylist.php?query=' + $(this).html(),
success: function(data) {
$('#divajax').html(data);
percentage = Math.round((i / total) * 100);
$('#loadingmsg').html('<h3>Adding songs...please wait<br>' + i + ' / ' + total + ' (' + percentage + '%)</h3>');
}
});
i++;
});
$('#loadingscreen').hide();
}
i hope you can help me out with this, i have no idea why the loading screen gets opened so late..
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在发出异步请求,但同步处理加载屏幕。您需要在 success 函数中删除此 div,并且您可能需要某种方法来计算仍然有多少个打开的请求。
You are making asynchronous requests, but dealing with the loading screen synchronously. You need to remove this div in the success function, and you'll probably need some way of counting how many requests you still have open.
如果你愿意的话,你可以像这样拥有它。
You can have it like this if you want.
您可以在ajax调用之后使用always函数,将它们链接在一起,然后当所有ajax调用完成时,关闭div。这可以使用指定的always 函数内的计数器来完成。
例子:
You can use the always function after the ajax calls, chain them altogether, and then when all of the ajax calls are finished, close the div. This can be accomplished using a counter inside the always function specified.
Example: