Da to know the response time of an AJAX request with jQuery?

3

I wanted to know if there is a function that returns the AJAX request time in microseconds, as I need to get this value for the project I'm developing.

    
asked by anonymous 29.07.2017 / 00:32

1 answer

2

You need to store a timestamp at the time of the request, get another at the time of the response, and compare the two:

var inicio = performance.now();
$.get('https://httpbin.org/get').done( function(response) {
    var tempo = performance.now() - inicio;
    console.log('A requisição levou ' + tempo + 'ms');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Time is in milliseconds, but note that it has decimal places. This is only possible with performance.now() , with no support in old browsers, instead of Date.getTime() , which has no support issues.

    
29.07.2017 / 00:53