I am trying to use javascript on a submit button. Upon hitting the submit button, I run a routine that does a remote callback with callback handler h. Basing on the result of h, I'll decide whether or not to proceed with the submit or to preventDefault and stay on the page.
I tried several approaches. First one is to run the submit using jquery after the callback:
$('#submit').on('click', function(e) {
e.preventDefault();
run_func_with_callback(h);
}
h() {
if (success) {
$('#submit').submit();
}
}
Another one is trying for an infinite loop until a flag is changed by the call back:
callback_flag = 0;
$('#submit').on('click', function(e) {
run_func_with_callback(h);
while(callback_flag == 0) {
setTimeout(function() {}, 1000); // I want some form of sleep function here
}
if (callback_flag == -1) {
e.preventDefault();
}
}
h() {
if (success) {
callback_flag = 1;
} else {
callback_flag = -1;
}
}
However, neither of these solutions work. First one I guess jQuery just doesn't really work that way. Second one the method I did was not a sleep function and does not behave in the way I need.
Any insights are welcomed. Thanks!