How to solve Google v3 reCaptcha timeout?
We've been running into this recently, too. I found this solution on GitHub and it's been working well for us.
This has the benefit of only asking for a token when the user interacts with the page (such as submitting a form) instead of having to keep asking for one.
<script>
grecaptcha.ready(function() {
document.getElementById('contactform').addEventListener("submit", function(event) {
event.preventDefault();
grecaptcha.execute('mykey', {action: 'homepage'}).then(function(token) {
document.getElementById("googletoken").value = token;
document.getElementById('contactform').submit();
});
}, false);
});
</script>
If you do not wish to change your code too much then an alternative approach would be to wrap the reCaptcha JavaScript in a named function, set up an interval and poll that function prompting reCaptcha to add a new token to your form element 10 seconds before each two minute token expiry:
function getReCaptcha(){
grecaptcha.ready(function() {
...
});
}
getReCaptcha(); // This is the initial call
setInterval(function(){getReCaptcha();}, 110000);