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.

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

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题