Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jquery how to measure response time for a server

Posted on 2013-01-18
3
Medium Priority
?
1,634 Views
Last Modified: 2013-01-21
Could someone help in telling how to calculate the response time of call in jquery. Basically I need to check how long a call took from server side to respond and then capture the timing for each call made.   Some sample code is appreciated.

There is a function which lets say submit a form process it and get the response from server, and I need to capture how long it took to finish the function.  e.g.

signup.AppController.prototype.submitApplication = function(view, submission, fields) {
    view.submitting();
    controller.submission = submission;

    $.ajax({
        type: "POST",
        url: url,
        contentType: 'application/json',
        data: JSON.stringify(submission),

        complete: function(jqXHR, textStatus) {
            var response        = jqXHR.responseText ? JSON.parse(jqXHR.responseText) : {},
                status          = jqXHR.status,
                doneSubmitting  = true;
           
            if (!status) {
                // blah blah
            }

            if (doneSubmitting)
                view.doneSubmitting();
        }
    })
}
0
Comment
Question by:ysgdel
  • 2
3 Comments
 
LVL 54

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 38796249
try this


    $.ajax({
var start = new Date().getTime(); //start timer
        type: "POST",
        url: url,
        contentType: 'application/json',
        data: JSON.stringify(submission),



        complete: function(jqXHR, textStatus) {
            var response        = jqXHR.responseText ? JSON.parse(jqXHR.responseText) : {},
                status          = jqXHR.status,
                doneSubmitting  = true;
var elapsed = new Date().getTime() - start; //end timer            
            if (!status) {
                // blah blah
            }

            if (doneSubmitting)
                view.doneSubmitting();
        }
    })
}
0
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 38796334
If you are asking about being able to display on the page, you need to start a timer before by creating a "start" variable with the current time and another "end" by recording the current time when the function is called.   What I gave you should be the timing from submitting the call to receiving.  It would be interesting to do the same serverside where you record the time serverside at the top of your serverside page and then again at the bottom and return the processed time.  Then in your main page you could have something like, "This process took xx milliseconds to process and xx milliseconds to load where the first number is your serverside timing and the 2nd is the js timing.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question