是否可以向此循环添加回调?作为一种让应用程序知道“现在是时候执行此操作了..”的一种方式..?
$(xml).find("book").each(function(){
var id = $(this).find("id").text();
var x = $(this).find("x").text();
var y = $(this).find("y").text();
var z = $(this).find("z").text();
hasItem(id, x, y, z, function(flag, id, x, y, z) {
if(!flag) {
//doing something
}
});
});
//add callback to tell me that all rows of xml has been read and hasItem is done reading?
最佳答案
即使这是一个异步操作,为什么不在完成后调用一个函数呢?
function done(){
// completed...
}
var total = $(xml).find("book").length;
$(xml).find("book").each(function(index, element){
var id = $(this).find("id").text();
var x = $(this).find("x").text();
var y = $(this).find("y").text();
var z = $(this).find("z").text();
hasItem(id, x, y, z, function(flag, id, x, y, z) {
if(!flag) {
//doing something
if(total == index+1)
done();
}
});
});
关于javascript - 将回调添加到 $(xml).find ("..").each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15116857/