我如何从一个jQuery Ajax调用的响应时间?响应时间、jQuery、Ajax

2023-09-10 17:38:48 作者:我是學渣又是情渣

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

我用这样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的返回值。

jquery ajax的2种写法 一种能进action 一种进不去 求解

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.

 
精彩推荐
图片推荐