Answer the question
In order to leave comments, you need to log in
How to stop the loop before ajax is done?
Hello, how to stop the loop before ajax execution and continue after execution? The response of one cycle is about 5 seconds, there can be up to 40 cycles, it is necessary that the browser does not freeze.
jsonData.checkList.forEach(function(data) {
$.ajax({
type: 'POST',
url: '/action/action.php',
data: data,
success: function(response) {
var jsonData = JSON.parse(response);
.......
}
});
});
Answer the question
In order to leave comments, you need to log in
In JavaScript, a loop for
can wait for the result of a promise at each iteration.
for (data of datas) {
await fetch( ... )
}
As easy as pie. Run in console:
for (key in jsonData.checkList) {
var response = await $.ajax({
type: 'POST',
url: '/action/action.php',
data: jsonData.checkList[key],
dataType: 'json' // Указываем, что ответ надо сразу распарсить, как JSON
});
console.log('Получены данные:');
console.dir(response);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question