Can you tell the response time of an AJAX request with jQuery?

Asked

Viewed 474 times

3

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

  • Do you need to do this via code? Because browser developer tools show the time.

  • That’s right, I’ll use the code.

1 answer

2


You need to save a timestamp from the time of the request, get another at the time of the reply, 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>

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.