Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 716
Ideal Bakija Avatar asked Mar 07 '26 17:03

Ideal Bakija


1 Answers

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.

like image 164
Jivings Avatar answered Mar 10 '26 06:03

Jivings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!