admin管理员组文章数量:1415684
I've an ajax call to set of urls to get html content. But some of the urls are taking long time to get the content, i'm okay to skip those which take more than say 1 sec.
for (url in list) {
$.ajax({
'url': url,
type: 'get',
timeout: 1000,
success: function () {
//doSomething
},
plete: function () {
//what to write?
}
});
}
Can you help me in how to abort the ajax call if it timeouts and execute the plete function.
Lemme know if i'm not clear. Thanks
I've an ajax call to set of urls to get html content. But some of the urls are taking long time to get the content, i'm okay to skip those which take more than say 1 sec.
for (url in list) {
$.ajax({
'url': url,
type: 'get',
timeout: 1000,
success: function () {
//doSomething
},
plete: function () {
//what to write?
}
});
}
Can you help me in how to abort the ajax call if it timeouts and execute the plete function.
Lemme know if i'm not clear. Thanks
Share Improve this question edited Sep 13, 2012 at 17:17 Sushanth -- 55.8k9 gold badges69 silver badges107 bronze badges asked Sep 13, 2012 at 16:54 RaviTejaRaviTeja 1,0263 gold badges14 silver badges20 bronze badges2 Answers
Reset to default 2First, change $.ajax(function() {
to $.ajax({
, because the argument you are trying to use is an object, not a function.
Since you specify a timeout in your options object, if the page doesn't respond in time, the ajax method will tell your error
function so, as long as you specify one.
$.ajax({
'url': yourUrlHere,
success: function(){ /*do stuff*/ },
timeout: 1000,
error: function(xhr, status, err){
//status === 'timeout' if it took too long.
//handle that however you want.
console.log(status,err);
}
});
See the documentation for $.ajax
here, as it has a pretty good explanation of the function.
You can abort the Request if that happens and go on to the next one..
var ajaxRequests = null;
for(url in list){
var newRequest = $.ajax({
type: 'GET',
url: url,
data: '{}',
dataType: 'json',
beforeSend: function() {
var r = ajaxRequest;
if (r != null && r != undefined) {
r.abort();
}
},
success: function(result) {
// what has to be done if the ajax request es through
},
plete: function() {
ajaxRequests = null;
}
ajaxReqs = newRequest;
}
本文标签: javascriptJquery ajax call timeoutStack Overflow
版权声明:本文标题:javascript - Jquery ajax call timeout. - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745222863a2648474.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论