下面是我的函数的示例代码.在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; } }); }
我发现有时需要更多的时间来显示价格.我想检查哪个记录需要时间来加载.如何检查以检测加载价格需要超过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 });
顺便说一下,如果你想阻止发送(i + 1)请求,在(i)完成之前,你可能想要使用同步的ajax请求而不是异步.