有什么方法可以查看 JQuery AJAX 调用执行需要多长时间?

发布于 2024-09-13 23:44:07 字数 122 浏览 0 评论 0原文

无论如何,是否可以查看在 jquery/javascript 中使用某种类型的计时代码调用一个简单的 $.getJSON 方法需要多长时间?

其次,有什么方法可以查看响应内容长度有多大(以 kB 或兆字节为单位)?

Is there anyway to see how long a simple $.getJSON method takes to call using some type of timing code, in jquery/javascript?

Secondly, is there any way to see how BIG the response Content-Length is, in kB or megabytes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦过后 2024-09-20 23:44:07

如果您想要纯 JavaScript,请在​​发送请求之前使用 记下时间new Date().getTime();

然后在ajax回调中,再次记下时间,并减去第一次的时间。这将为您提供通话时长。

像这样的事情:

function aCallback()
{
   window.time2 = new Date().getTime();
   window.alert(window.time2 - window.time1)
}
window.time1 = new Date().getTime();
$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: aCallback
});

If you want pure javascript, right before you send the request note the time using new Date().getTime();

Then in the ajax callback, note the time again, and subtract against the first time. That will give you the length of the call.

Something like this:

function aCallback()
{
   window.time2 = new Date().getTime();
   window.alert(window.time2 - window.time1)
}
window.time1 = new Date().getTime();
$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: aCallback
});
小耗子 2024-09-20 23:44:07

要回答第二个问题,您可以从 XHR 响应中读取 Content-Length 标头。

var req;
req = $.ajax({
    type: "HEAD",
    url: "/data",
    success: function () {
      alert("Size is " + req.getResponseHeader("Content-Length"));
    }
});

To answer your second question, you can read the Content-Length header from the XHR response.

var req;
req = $.ajax({
    type: "HEAD",
    url: "/data",
    success: function () {
      alert("Size is " + req.getResponseHeader("Content-Length"));
    }
});
因为看清所以看轻 2024-09-20 23:44:07

为什么要重新发明轮子?使用 firebug 的控制台记录时间。执行如下操作:

function aCallback()
{
   console.timeEnd("ajax call");
   window.alert(window.time2 - window.time1)
}
console.time("ajax call");
$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: aCallback
});

这将记录服务器响应返回调用所需的时间。它将把时间(以毫秒为单位)记录到 Firebug 控制台。

Why reinvent the wheel? Use firebug's console to log the time. Do something like this:

function aCallback()
{
   console.timeEnd("ajax call");
   window.alert(window.time2 - window.time1)
}
console.time("ajax call");
$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: aCallback
});

This will log how long it took until the server response gets back to the call. It will log the time in miliseconds to the firebug console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文