所有 ajax 调用完成后显示加载屏幕

发布于 2024-10-12 17:43:33 字数 900 浏览 4 评论 0原文

嘿, 我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里的微风 2024-10-19 17:43:33

您正在发出异步请求,但同步处理加载屏幕。您需要在 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.

葮薆情 2024-10-19 17:43:33

如果你愿意的话,你可以像这样拥有它。

function addAllToPlaylist() {
    var title;
    var i = 1;
    var percentage = 0;
    var total = $('.tdtrackname').size();
     $('#loadingscreen').show();

    percentage = Math.round((i / total) * 100);
            $('#loadingmsg').html('<h3>Adding songs...please wait<br>' + i + ' / ' + total + ' (' + percentage + '%)</h3>');

     $('.tdtrackname').each(function() {
        $.ajax({
        async: false,
        url: 'ajax/addsongtoplaylist.php?query=' + $(this).html(),
        success: function(data) {
            $('#divajax').html(data);
            $('#loadingscreen').hide();
        }
      });
      i++;
     });

}

You can have it like this if you want.

function addAllToPlaylist() {
    var title;
    var i = 1;
    var percentage = 0;
    var total = $('.tdtrackname').size();
     $('#loadingscreen').show();

    percentage = Math.round((i / total) * 100);
            $('#loadingmsg').html('<h3>Adding songs...please wait<br>' + i + ' / ' + total + ' (' + percentage + '%)</h3>');

     $('.tdtrackname').each(function() {
        $.ajax({
        async: false,
        url: 'ajax/addsongtoplaylist.php?query=' + $(this).html(),
        success: function(data) {
            $('#divajax').html(data);
            $('#loadingscreen').hide();
        }
      });
      i++;
     });

}
半边脸i 2024-10-19 17:43:33

您可以在ajax调用之后使用always函数,将它们链接在一起,然后当所有ajax调用完成时,关闭div。这可以使用指定的always 函数内的计数器来完成。

例子:

var methodCounter = 0;

$.ajax({
  success: function(d) {
  },
  url: 'xxx',  //Where xxx is the url being hit
}).always(function() {
  methodCounter++;
  if (methodCounter == limit) { //limit is the number of times to do the ajax call before closing the dialog box
    //Hide the dialog box here
  }
});

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:

var methodCounter = 0;

$.ajax({
  success: function(d) {
  },
  url: 'xxx',  //Where xxx is the url being hit
}).always(function() {
  methodCounter++;
  if (methodCounter == limit) { //limit is the number of times to do the ajax call before closing the dialog box
    //Hide the dialog box here
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文