dongmu1989 2014-05-13 04:51 采纳率: 0%
浏览 241
已采纳

带百分比的JQuery Ajax请求

I need to get the % completion for ajax request.

I tried the following:

$(document).ready(function() {
    $.ajax({
        xhr : function() {
            var xhr = new window.XMLHttpRequest();
            xhr.upload.addEventListener("progress", function(evt) {
                if (evt.lengthComputable) {
                    var percentComplete = (evt.loaded / evt.total) * 100;
                    //Do something with upload progress here
                }
            }, false);

            xhr.addEventListener("progress", function(evt) {
                if (evt.lengthComputable) {
                    var percentComplete = (evt.loaded / evt.total) * 100;
                    //Do something with download progress
                    console.log(percentComplete);       
                }
            }, false);

            return xhr;
        },
        url : my_path
    }).done(function(data) {

        console.log(data);
    });
}); 

The problem is how do I check whether the above code is working or not. I always get 100 in the firebug console, I doubt instead of 100 there should be multiple % entries in console.

my_path is a PHP page that returns records from MySQL DB. Is there any way to slow down the process/page rendering to check the functionality?

  • 写回答

1条回答 默认 最新

  • douhutongvm382381 2014-05-14 21:38
    关注

    The problem with using DB reads is the size needs to be known first, so you really need a file to test this.

    For Downloading (client downloading off from the server), you need to introduce delay, otherwise you're only recourse is to download a huge file.

    Try this script, which sets up the correct content length headers and only outputs 50 bytes at a time, with a 1 second delay (you can test it with a relatively small file, woot).

    As for Uploading (posting to the server), your only option in this regard is to make it a big file, and discard it when done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料