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.
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.
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.