Php 当一个Ajax on success回调完成时,是否从jQuery When函数继续另一个回调?
有没有一种方法可以在jquery“when”函数中的下一个Ajax调用完成后(可能是回调之类的)调用它。 到目前为止,我还把这个例子放在 本例中本地主机爬虫的URL数组Php 当一个Ajax on success回调完成时,是否从jQuery When函数继续另一个回调?,php,jquery,python,ajax,web-crawler,Php,Jquery,Python,Ajax,Web Crawler,有没有一种方法可以在jquery“when”函数中的下一个Ajax调用完成后(可能是回调之类的)调用它。 到目前为止,我还把这个例子放在 本例中本地主机爬虫的URL数组 var iterations = ['//localhost?p=1', '//localhost?p=2', '//localhost?p=3']; 将保留每个ajax调用 var xhr = []; 进行迭代 $.each(iterations, function(url, k){ // store every
var iterations = ['//localhost?p=1', '//localhost?p=2', '//localhost?p=3'];
将保留每个ajax调用
var xhr = [];
进行迭代
$.each(iterations, function(url, k){
// store every ajax function in the array
xhr.push(
$.ajax({
url: url,
cache: false,
//async: false,
dataType: "html",
success: onSuccess
})
);
})
// then must be called when all onSuccess functions are done completed (every onsuccess have their own ajax call as well)
$.when.apply($, xhr).then(function(){
alert('This must be called when all xhrs have been triggered');
})
触发此函数内的AJAX调用后,继续下一次xhr迭代。有没有办法对onSuccess函数进行回调
function onSuccess(data){
$.ajax({
url: 'http://localhost',
cache: false,
//async: false,
dataType: "html",
success: function(){
alert("on success 2, continue with the next xhr iteration");
},
error: function(){
alert("on error 2, continue with the next xhr iteration");
}
})
}
以下是您如何做到这一点:
使用此方法,一次只运行一个请求。如果您希望同时处理所有请求,则需要另一个数组来跟踪每个请求的状态。每次请求完成时,通过循环检查您的状态数组,查看它们是否都已完成。一个好的选择是创建一个类或函数来处理此类处理。你可以用几种方法来做。假设你用一个实用类来做这个。。。我可以想象它有一个构造函数,
start()
,和\u next()
构造函数将接收并保存URL数组
start()
将触发一个AJAX请求,并成功(可能是错误)回调到\u next()
\u next()
将查看URL数组,并选择下一个URL(这可以通过将它们移出数组或使用内部指针/头来完成;然后\u next()
将再次使用\u next()
进行另一个AJAX调用,作为成功/错误回调
最后,当没有更多URL可调用时,\u next()
中需要有一个终止案例