简体   繁体   中英

Progress Bar on PushState

This is the code:

$.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){

        }
    });

This is the function I use to retrieve my view from Codeigniter. I want to show a progress bar in the meantime while the page is loading. However when I put the XHR in the AJAX funtion everything stops working. Does anyone have an idea how can I make it run with a progress bar.

Thanks in Advance.

You've missed out a variable declaration so your code doesn't compile.

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;
},

Note that if evt.lengthComputable is false then you cannot measure the progress of the request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM