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 am using to retrieve my view from Codeigniter. I want to show a progress bar while the page is loading. However, when I put XHR in an AJAX function, everything stops working. Does anyone have an idea how I can get it to work with a progress bar.

Thanks in Advance.

+3


source to share


1 answer


You left out the variable declaration to prevent your code from compiling.

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

      



Please note that if evt.lengthComputable

false, you cannot measure the progress of the request.

+2


source







All Articles