php – Returning Values ​​During Ajax Request

Question:

Dear visitors, I am faced with a difficulty, I need your help.

Essence of the question:

  • When processing an ajax request, a lot of information is loaded from php;
  • For the visibility of the download, I made a preloader of the page, but now I decided to make a progressbar with the display of the value %% of the download / the number of bytes transferred, it doesn't matter;
  • I thought to get the progress values ​​in a loop and output them to a container with a specific id / class.

The crux of the problem:

  • The var progress value, for example, is evt.loaded to the console only after the request has been executed, and not every time the evt.loaded value evt.loaded (the amount of information loaded).

The code:

$(document).ready(function () {
    $.ajax({ 
        xhr: function() {
            var xhr = new window.XMLHttpRequest();

            xhr.addEventListener("progress", function(evt){
                for(var i = 1; i<evt.total; i++){
                    var progress = Math.round(((i/evt.total)*100)*100)/100;
                    //действие, вывод значения прогресса (консоль / контейнер)          
                }
            }, false);
            return xhr;
        },         
        type: "POST",
        url: "main_php/result.php",
        dataType: "json",
        success: function (data) {
            //действие (вывод данных на страницу)
        }
    });
});

Answer:

in percentages, most likely you will not be able to get it, but throw some anima on

onsend()

and end it on

success()
Scroll to Top