Steve Kim
Steve Kim

Reputation: 5611

JS measuring the ajax completion time

I have a simple ajax request for js and php (fetching data from mysql)

function ajax_function(a,b) {
    $.ajax({
        type : 'POST',
        url : mine.ajax_url,
        dataType : 'json',
        data : {
            action : a,             
        },
        success : b
    });
};

How would I detect the time that it takes from the initial request to the completion? (I am trying to show progress bar).

Thanks!

Upvotes: 2

Views: 70

Answers (1)

Iceman
Iceman

Reputation: 6145

If you want progress status:

xhr: function () {
    var xhr = $.ajaxSettings.xhr(); // call the original function
    xhr.addEventListener('progress', function (e) {
        if (e.lengthComputable) {
            var percentComplete = e.loaded / e.total;
            // Do something with download progress
        }
    }, false);
    return xhr;
}

Upvotes: 2

Related Questions