ASP/AJAX - How to get the time between an server request and response?

て烟熏妆下的殇ゞ 提交于 2019-12-25 02:49:22

问题


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.



来源:https://stackoverflow.com/questions/2728447/asp-ajax-how-to-get-the-time-between-an-server-request-and-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!