如何检查json响应耗时超过5秒
How to check json response taken longer than 5 seconds?
下面是我的函数的示例代码。在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)完成之前阻止发送(i+1)请求,您可能希望使用syncronous ajax请求而不是async。
尝试记录时间戳beforesend
和success
或error
$.ajax({ // ajax call starts
url: 'product.php',
data: { product_id: doc.id },
dataType: 'json',
beforeSend: function() {
console.log(new Date().getSeconds());
}
success: function(data)
{
console.log(new Date().getSeconds());
document.getElementById('price_price'+data.product_id+'').innerHTML = data.products_price;
}
});
使用setTimeout,如下所示:
var timeoutTimer = setTimeout(function() {
// time out!!!.
}, 5000);
$.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;
},
complete : function() {
//it's back
clearTimeout(timeoutTimer);
}
});
相关文章:
- 显示5秒后隐藏潜水
- 如何将getJson的响应保存在全局变量中
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 使用angularjs向浏览器发送servlet响应(下载功能)
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 不再使用innerHTML进行AJAX响应.(代码未得到响应)
- 为什么可以't我使用了AJAX响应的一部分
- X秒后刷新select元素
- 响应动画手风琴不工作
- 如何从SeleniumWebdriver获取异步Javascript响应
- ajax请求每秒返回10个响应
- Chrome在刷新应用程序后30秒以上没有响应(打开了开发工具)
- 如何检查json响应耗时超过5秒
- 运行 ajax 函数以每 3 秒检查一次 json 响应
- 如何延迟 AJAX 响应秒数
- 每 XX 秒调用一次 GET API,并将 JSON 响应放入 CSV 中
- 如何使用javascript记录两次按钮按下之间的响应时间(以秒为单位)
- 需要xml数据来保持每2秒POST一次,直到收到响应
- 烬:每5秒轮询一次api,但要在获得前一次调用的响应之后
- 如何每隔20秒加载$.ajax(jquery)响应到iframe