获取AJAX请求持续时间 [英] Getting AJAX Request Duration

查看:819
本文介绍了获取AJAX请求持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery获取某个ajax请求的持续时间.例如,我正在使用此脚本:

I would like to get the duration for a certain ajax request using jquery. For instance I am using this script:

$.ajax({
  type: "GET",
  url: "http://localhost/thescript.php",
  success: function(data){
    $("#container").html(data);
  },
  complete: function(jqXHR, textStatus){
    alert("http://localhost/thescript.php took 'n' seconds to load");
  }
});

我想知道这个Ajax脚本加载URL"http://localhost/thescript.php"多长时间.例如,警报:"http://localhost/thescript.php的加载时间为'n'秒".

I would like to be able to know how long this ajax script loaded the url "http://localhost/thescript.php". For instance alert: "http://localhost/thescript.php took 'n' seconds to load".

推荐答案

您可以使用beforeSend()complete()函数比较当前时间戳并计算差异.

You can use the beforeSend() and complete() functions to compare the current timestamps and calculate the difference.

http://api.jquery.com/jQuery.ajax/

在这里您可以看到$.ajax提供的所有回调挂钩: http://api.jquery.com/jQuery.ajax/#callback-functions

Here you can see all callback hooks provided by $.ajax: http://api.jquery.com/jQuery.ajax/#callback-functions

这篇关于获取AJAX请求持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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