Find Out How Long an Ajax Request Took to Complete

Find out how long an Ajax request took to complete

@codemeit is right. His solution looks something like the following using jQuery for the ajax request. This returns the request time in milliseconds.

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

jQuery.get('your-url', data, function(data, status, xhr) {
var request_time = new Date().getTime() - start_time;
});

How do I get the response time from a jQuery ajax call?

The most simple method would be to add var ajaxTime= new Date().getTime(); before the Ajax call and in the done get the current time to calculate how long the Ajax call took to make.

var ajaxTime= new Date().getTime();
$.ajax({
type: "POST",
url: "some.php",
}).done(function () {
var totalTime = new Date().getTime()-ajaxTime;
// Here I want to get the how long it took to load some.php and use it further
});

Or in case of you want to know how long time this take on the server side.
Do the same and print the time in the return value from some.php.

Getting AJAX Request Duration

You can use the beforeSend() and complete() functions to compare the current timestamps and calculate the difference.

http://api.jquery.com/jQuery.ajax/

Here you can see all callback hooks provided by $.ajax:
http://api.jquery.com/jQuery.ajax/#callback-functions

how to get length of jQuery .load AJAX request

Just before the start of the .load, store the current time: startTime = new Date();. In the callback, get the current time again, and subtract the start time. You'll get the elapsed time in milliseconds.

how to get a time that a requests takes to complete?

Take note of the time before the request and after the request. The difference will be the time taken to complete the request.

var start = new Date().getTime(),
difference;

$.ajax({
url: url,
type: "POST",
data: {
name: "John",
location: "Boston"
}
}).done(function () {
difference = new Date().getTime() - start;
});

how to get a time that a requests takes to complete?

Take note of the time before the request and after the request. The difference will be the time taken to complete the request.

var start = new Date().getTime(),
difference;

$.ajax({
url: url,
type: "POST",
data: {
name: "John",
location: "Boston"
}
}).done(function () {
difference = new Date().getTime() - start;
});

How long will the browser wait after an ajax request?

If you are using a jQuery $.ajax call you can set the timeout property to control the amount of time before a request returns with a timeout status. The timeout is set in milliseconds, so just set it to a very high value. You can also set it to 0 for "unlimited" but in my opinion you should just set a high value instead.

Note: unlimited is actually the default but most browsers have default timeouts that will be hit.

When an ajax call is returned due to timeout it will return with an error status of "timeout" that you can handle with a separate case if needed.

So if you want to set a timeout of 3 seconds, and handle the timeout here is an example:

$.ajax({
url: "/your_ajax_method/",
type: "GET",
dataType: "json",
timeout: 3000, //Set your timeout value in milliseconds or 0 for unlimited
success: function(response) { alert(response); },
error: function(jqXHR, textStatus, errorThrown) {
if(textStatus==="timeout") {
alert("Call has timed out"); //Handle the timeout
} else {
alert("Another error was returned"); //Handle other error type
}
}
});​

AJAX response time

You need to get the start time (just before the AJAX request is done), and then the end time when the script is complete. You can than work out the difference, and if it's greater than 60 seconds, do your thing.

//Before the AJAX function runs
var startTime = new Date().getTime();

//Place this code inside the success callback of your AJAX function
var endTime = new Date().getTime();
if ((endTime - startTime) > (60 * 1000)) {
//Took longer than 60 seconds
}


Related Topics



Leave a reply



Submit