如何从jQuery ajax调用获取响应时间?

参见英文答案 > Find out how long an Ajax request took to complete5个
所以我正在努力工作,可以显示很长时间一个页面的请求.

我这样做是通过使用jQuery Ajax(http://api.jquery.com/jQuery.ajax/),我想找出最好的方式来获得响应时间.

我发现一个线程(http://forum.jquery.com/topic/jquery-get-time-of-ajax-post)描述了在JavaScript中使用“Date”,但这种方法真的可靠吗?

我的代码的例子可以在下面

$.ajax({
    type: "POST",url: "some.PHP",}).done(function () {
    // Here I want to get the how long it took to load some.PHP and use it further
});

解决方法

最简单的方法添加var ajaxTime = new Date().getTime();在Ajax调用之前,在完成后,得到当前时间来计算Ajax调用所花费的时间.
var ajaxTime= new Date().getTime();
$.ajax({
    type: "POST",}).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
});

或者如果您想知道服务器端需要多长时间.执行相同操作,并从some.PHP的返回值中打印出时间.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...