仅在前一个函数完全执行后执行Javascript函数
我有两个函数,doFirst()和doSomethingElse()。现在,第一个函数由大约10个AJAX请求组成,这些请求在检查是否选中了过滤器的条件下执行。这些AJAX请求与URL通信,并将一组ID保存在各自的阵列中。完成此操作后,必须启动doSomethingElse()以绘制结果表。此时,我通过设置一个超时来执行它们。我想知道一种更好的方法来等待函数完成,然后执行下一个函数。非常感谢您的帮助。 谢谢仅在前一个函数完全执行后执行Javascript函数,javascript,jquery,html,ajax,asynchronous,Javascript,Jquery,Html,Ajax,Asynchronous,我有两个函数,doFirst()和doSomethingElse()。现在,第一个函数由大约10个AJAX请求组成,这些请求在检查是否选中了过滤器的条件下执行。这些AJAX请求与URL通信,并将一组ID保存在各自的阵列中。完成此操作后,必须启动doSomethingElse()以绘制结果表。此时,我通过设置一个超时来执行它们。我想知道一种更好的方法来等待函数完成,然后执行下一个函数。非常感谢您的帮助。 谢谢 //我当前调用函数的方式: doFirst(); doSomethingElse();
//我当前调用函数的方式:
doFirst();
doSomethingElse();
函数doFirst(){
var filterayraytaxsel1=$(“#taxIA span”).get().map(el=>el.textContent);
对于(变量i=0;i0){
如果(filterArrayTaxSel1.length>0&&filterArrayTaxSel1[0]!=“全部”){
log(“我在IF里面!”);
对于(变量i=0;i
Promise
只是一个对象,您可以向其传递一些执行某些工作的函数,当它完成时,将调用回调函数-w
//How I am currently calling the functions:
doFirst();
doSomethingElse();
function doFirst(){
var filterArrayTaxSel1 = $("#taxIA span").get().map(el => el.textContent);
for (var i = 0; i < filterArrayTaxSel1.length; i++) {
filterArrayTaxSel1[i] = filterArrayTaxSel1[i].replace(" ", "%20");
}
// taxgroup2 - Selector
queryBuilder = filterArrayTaxSel1;
//console.log(queryBuilder);
console.log(filterArrayTaxSel1);
if (filterArrayTaxSel1.length > 0) {
if (filterArrayTaxSel1.length > 0 && filterArrayTaxSel1[0] != "All") {
console.log("I am inside the IF!");
for (var i = 0; i < filterArrayTaxSel1.length; i++) {
var baseURL = "some URL here";
console.log(baseURL);
responses(baseURL);
function responses(baseURL) {
$.ajax({
url: baseURL,
type: "get",
cache: false,
headers: {
'Content-Type': 'application/json'
},
success: function (data) {
console.log(data.features.length);
for (var i = 0; i < data.features.length; i++) {
if (taxArrayT1.indexOf(data.features[i].properties.taxon_id) == -1) {
taxArrayT1.push(data.features[i].properties.taxon_id);
}
}
console.log("In the Invertebrate Animals Section 1");
console.log(taxArrayT1.length);
}
})
}
}
}
else if (filterArrayTaxSel1[0] == "All") {
console.log("I am inside the IF!");
var baseURL = "some URL here";
console.log(baseURL);
responses(baseURL);
function responses(baseURL) {
$.ajax({
url: baseURL,
type: "get",
cache: false,
headers: {
'Content-Type': 'application/json'
},
success: function (data) {
console.log("I am inside the ELSE IF ALLL!");
console.log(data.features.length);
for (var i = 0; i < data.features.length; i++) {
if (taxArrayT1.indexOf(data.features[i].properties.taxon_id) == -1) {
taxArrayT1.push(data.features[i].properties.taxon_id);
}
}
console.log("In the Invertebrate Animals Section 2");
console.log(taxArrayT1.length);
}
})
}
}
//Selection 1 Tax Group AJAX Call Sensitivity ARRAY WITH 0 to Multiple CASES - ENDS.
}
//some more AJAX Calls depending on whether the filter exists
//End of function
}
function doSomethingElse(){
//Code to draw the Table using the arrays from the previous function
}
function makeAjaxCall(url, methodType){
var promiseObj = new Promise(function(resolve, reject){
var xhr = new XMLHttpRequest();
xhr.open(methodType, url, true);
xhr.send();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4){
if (xhr.status === 200){
console.log("xhr done successfully");
var resp = xhr.responseText;
var respJson = JSON.parse(resp);
resolve(respJson);
} else {
reject(xhr.status);
console.log("xhr failed");
}
} else {
console.log("xhr processing going on");
}
}
console.log("request sent succesfully");
});
return promiseObj;
}
let promise1 = makeAjaxCall(URL1, "GET");
let promise2 = makeAjaxCall(URL2, "GET");
let promise3 = makeAjaxCall(URL2, "GET");
Promise.all([promise1, promise2, promise3]).then(function(values) {
console.log(values);
});
function firstStep() {
var requests = []
var list = [1,2,3]
for (var i = 0; i < list.length; i++) {
requests.push($.ajax(...));
}
return requests
}
function secondStep() {
console.log(arguments)
}
var requests = firstStep()
$.when.apply(undefined, requests).then(secondStep)