gpt4 book ai didi

javascript - 在多次ajax请求后执行一个函数

转载 作者:行者123 更新时间:2023-12-02 14:12:09 26 4
gpt4 key购买 nike

我尝试在 6 次 ajax 调用后执行一个函数(包括每次调用后的“.done”部分)

function showReports(array_monitor){
$.ajax({
method: "GET",
url: array_monitor[0],
datatype: "text",
}).done(function(xml) {
monitorName = array_monitor[1];

convert(xml, monitorName);
});
};

我尝试过 $.when 但似乎没有按我想要的方式工作

编辑:

我已经包含了一个全局计数器,并且在所有调用都必须压缩创建的一些文件之后的函数,是否需要设置等待超时或 $.when 足够?

function zipAll(){
$.ajax({
method: "POST",
url: "zipAll.php",
}).done(function(){
window.location = "uptimeReports.zip";
console.log("hola");
});
}

最佳答案

$.ajax 返回一个类似 Promise 的对象。您将其(全部 6 个)提供给 $.when。另外,为了保持一致性,不要使用 .done 而是使用 .then,因为 Promise 使用 .then

function showReports(array_monitor){
return $.ajax(...).then(function(xml){
// Do some parsing
return finalValue;
});
}

$.when.apply(null, [
showReports(...),
showReports(...),
showReports(...),
showReports(...),
showReports(...),
showReports(...),
]).then(function(result1, result2,...){
// All done
});

还要确保从 showReports.then 回调返回一个值。它是 Promise 的解析值,它成为传递到 $.when 的值。

关于javascript - 在多次ajax请求后执行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39449703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com