javascript - 获取 AJAX 请求持续时间

标签 javascript jquery ajax

<分区>

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

$.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’秒”。

最佳答案

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

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

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

关于javascript - 获取 AJAX 请求持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5257259/

相关文章:

jquery - asp.net mvc 所见即所得编辑器/数据库图像上传/预览

javascript - 在 jquery 中迭代 json

javascript - 如何在输入字段中搜索特定单词

javascript - 检索 firestore 文档 ID 并添加到 html

javascript - 使用 jQuery/Javascript 处理 HTML 元素组?

php - 在 ajax 调用后加载 View ,CodeIgniter

c# - 使用 asp.net mvc 的异步操作调用

javascript - 确定带有类的部分是否可见,jQuery

javascript - Object.assign 的意外输出

jquery - 从 ColdFusion 调用 jQuery