PushState上的进度条

时间:2014-11-04 16:11:11

标签: javascript jquery ajax codeigniter

这是代码:

$.ajax({                        
        type: "POST",
        url: url,
        xhr: function(e) {
            //Download progress         
            xhr.addEventListener("progress", function(evt){
                console.log(evt.lengthComputable);
              if (evt.lengthComputable) {
                var percentComplete = evt.loaded / evt.total;
                //Do something with download progress
                console.log(percentComplete);
              }
            });
            return xhr;
        },
        dataType: "json",
        success: function(data){

        }
    });

这是我用来从Codeigniter检索我的视图的函数。我想在页面加载的同时显示进度条。然而,当我将XHR放入AJAX函数时,一切都停止了。有谁知道如何使用进度条运行它。

先谢谢。

1 个答案:

答案 0 :(得分:2)

您错过了变量声明,因此您的代码无法编译。

xhr: function () {
    var xhr = new window.XMLHttpRequest(); // <<<<<<< missed this variable
    //Download progress
    xhr.addEventListener("progress", function (evt) {
        if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            //Do something with download progress
            console.log(percentComplete);
        }
    }, false);
    return xhr;
},

请注意,如果evt.lengthComputable为false,则无法衡量请求的进度。

相关问题