ASP/AJAX - How to get the time between an server r

2019-08-22 16:07发布

问题:

Whenver Ajax requests new data from the server this can sometimes take a a second or two. Now I want to know, how can I get this time between the ajax request and the response it gets from the server?

I need this because an ajax timer I'm running ain't perfectly doing his stuff. It got some delay whenever it needs to reset to it's original time.

Thanks in Advance.

Edit: Help needed fast please, just try.

回答1:

Use Firebug to see the timings for each request. http://getfirebug.com/network



回答2:

This should work. Taken parts from the other example but with the ajax logic.

$(document).ready(function() {
    $('#start').click(function() {
        start_timer();
        $.ajax({url: "/serverside.aspx", success: function(data){ 
            stoptimer(); 
        }});
    }); 
});

var date1;
var start_timer = function(){ var date1 = new Date(); }
var stoptimer = function(){ 
    var date2 = new Date();
    alert('Request took: '+ date2.getTime()-date1.getTime()) 

}


回答3:

Such as getting the current time when making the request, doing the same when getting a response and getting the difference to know the elapsed time?

What specifically is your problem? You don't know the javascript for this? If you're in that much of a hurry im guessing google would be faster than SO.

Edit

var date1 = new Date();
setTimeout(getElapsed,1000);

function getElapsed()
{
var date2 = new Date();
alert(date2.getTime()-date1.getTime());
}

This throws an alert with "1008" on my machine - this equates to milliseconds.