Recaptcha v2 throws an error when it is reset and its container element is removed from DOM

前端 未结 2 1896
醉酒成梦
醉酒成梦 2021-02-04 08:10

When I explicitly render Recaptcha v2 and then reset it before removing it from the DOM, after ~40 seconds I get an error in the browser console.

I have a JSFiddle which

2条回答
  •  执念已碎
    2021-02-04 09:07

    After many hours I have found only this solution, see https://jsfiddle.net/4mLhcksq/

    There is setTimeout and 60 seconds (it could be shorter, I guess) pause before grecaptcha.reset() and then another pause before removing Recaptcha element. Actually, I had the same problem even if I do not remove Recaptcha element, only reset it.

    const holder = getRecaptchaHolder();
    holder.style.display = 'none'; //element disappears for users
    setTimeout(function() {
      grecaptcha.reset(recaptchaWidgetId);
      setTimeout(function() { //we have to wait a while before removing element
        holder.parentElement.removeChild(holder);
      }, 1000);
    }, 60000);
    

    I am not especially proud of this solution. Let us hope somebody will provide better one.

提交回复
热议问题