I'm using Google's reCAPTCHA on a form and after every submit I need it reset, because if the server-side validation fails and the user resubmits the same form, the captcha will be invalid. I'm using plain javascript, not using ajax/jquery, the form opens a new page (instead of reloading the current one). Here's my code:
document.getElementById('myform').addEventListener('submit', function(event) {
event.preventDefault();
this.submit();
setTimeout(function(){ grecaptcha.reset(myCaptcha); }, 0);
}
I'm using setTimeout
to put the reset code in the event loop queue.
My question is, will this work every time? Is submit()
synchronous, i.e. it will always execute before whatever is in the event loop queue?