This question has already been answered:
I make a function to check the correctness of the data when sending the form There is a variable error with error numbers. If error == 0, then send the form, if not, then output the corresponding error and return false. Inside the function there is a $ .get-function that does one of the checks (and if the data is incorrect, it changes the value of error). Problem: since the function is asynchronous, the assignment of a variable occurs already after the form is sent (that is, the check does not work). Question: how to pass the error value BEFORE inside the function check goes to error == 0. Or how to pause the form submission before the $ .get function is processed?
$("#button").submit(function () {// обрабатываем отправку формы var error = 0; // индекс ошибки ... //первая проверка (классическая, с ней всё хорошо) if (a > b) { error = 1; } ... $.getJSON('json.json', function (data) { ... if (...){ setResult("2"); // присваиваем error значение 2 } ... }); //присваиваем значение внутри get-функции function setResult(result) { error = result; } ... дальше проверки error, и return true, если error == 0 или вывод ошибок + return false. PS There is a wonderful answer here: How can I return a value from an event or from a callback function? - but something didn’t really help, because I don’t just have to pass the value, but pass it BEFORE checking for error == 0 and sending the form.