如何检查json resopnse花了超过5秒?

Jim*_*mmy 2 javascript php ajax jquery

下面是我的函数的示例代码.在for循环中逐个产品id传入ajax函数并从php文件中获取产品价格作为响应并写入它和html.

for(var i=0; i < data.products.length; i++){
 var doc = data.products[i];
 $.ajax({ // ajax call starts 
          url: 'product.php',
          data: { product_id: doc.id },
          dataType: 'json',
          success: function(data)
          {
           document.getElementById('price_price'+data.product_id+'').innerHTML = data.products_price;
          }
 });
 }
Run Code Online (Sandbox Code Playgroud)

我发现有时需要更多的时间来显示价格.我想检查哪个记录需要时间来加载.如何检查以检测加载价格需要超过5秒的时间?

我将不胜感激任何帮助.

Bog*_*rym 5

像这样......

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
});
Run Code Online (Sandbox Code Playgroud)

顺便说一下,如果你想阻止发送(i + 1)请求,在(i)完成之前,你可能想要使用同步的ajax请求而不是异步.