我如何从一个jQuery Ajax调用的响应时间? [英] How do I get the response time from a jQuery ajax call?

查看:682
本文介绍了我如何从一个jQuery Ajax调用的响应时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我工作的工具,它可以呈现多头请求一个页面正在

我用这样jQuery的阿贾克斯(http://api.jquery.com/jQuery.ajax/),我想弄清楚获得响应时间的最好方式。

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

我的code的例子可以参考下面

  $。阿贾克斯({
    键入:POST,
    网址:some.php
})。完成(功能(){
    //这里我要得到花了多长时间来加载some.php并进一步使用它
});
 

解决方案

最简单的方法是添加 VAR ajaxTime =新的日期()的getTime(); Ajax调用之前和完成获取当前时间来计算多久Ajax调用了做。

  VAR ajaxTime =新的日期()的getTime()。
$阿贾克斯({
    键入:POST,
    网址:some.php
})。完成(功能(){
    VAR totalTime =新的日期()的getTime() -  ajaxTime。
    //这里我要得到花了多长时间来加载some.php并进一步使用它
});
 

或者万一你想知道时间多久,这需要在服务器端。 做同样的打印时间从some.php的返回值。

So I am working on tool that can show long a request to a page is taking.

I am doing this by using jQuery Ajax (http://api.jquery.com/jQuery.ajax/) and I want to figure out the best way to get the response time.

I found a thread (http://forum.jquery.com/topic/jquery-get-time-of-ajax-post) which describes using the "Date" in JavaScript, but is this method really reliable?

An example of my code could be this below

$.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
});

解决方案

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.

这篇关于我如何从一个jQuery Ajax调用的响应时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆