I am submitting a form based on a condition and a setTimeout statement in submit event handler, however the form is still getting submitted even when I put return false in setTimeout function.
bool = true;
$('.form-example').submit(function(e) {
//some_stuff where I am get a value true or false in variable bool
setTimeout(function () {
if(bool) {
console.log('form shouldnt submit coz the following statement is return false');
return false;
}else{
return true;
}
}, 1000);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="post" class="form-example">
<div class="form-example">
<label for="name">Enter your name: </label>
<input type="text" name="username" id="username" required>
</div>
<input type="submit" name="submit_user">
</form>