Ion
Ion

Reputation: 21

Processing time ajax

In Ajax is possible to calculate the processing time of the request? For example, if the extraction of data takes less than 3 seconds, to display a message and to enroll in logs by ajax. If possible you show me an example. P.S. Sorry for my bad english

Upvotes: 0

Views: 67

Answers (2)

pistou
pistou

Reputation: 2867

You may want to use timers :

function ajaxFunction() {
    var startTime = new Date().getTime();

    $.ajax({
        type: "POST",
        url: "url.php",
        data: {},
        success: function(ret) {
            // script is done
            var endTime = new Date().getTime();
            var totalTime = endTime - startTime; // this is in milliseconds

            if (totalTime >= 3000) {
                console.log("Script took " + totalTime + " ms" );
            }
        }
    })
}

Note that getTime() returns milliseconds. So you'll have to check for 3000ms = 3s

Upvotes: 0

Ram Sharma
Ram Sharma

Reputation: 8819

To get response time you can try something like this but as per this example you have to use jQuery.

var starttime = new Date().getTime();

jQuery.get('your-url-goes-here', data, 
    function(data, status, xhr) {
        var requesttime = new Date().getTime() - starttime;
    }
);

Upvotes: 1

Related Questions