Skip to content

Commit 629d547

Browse files
on submit, refresh turnstile if the 5 minute timeout has elapsed
2 parents e898349 + 838cc79 commit 629d547

3 files changed

Lines changed: 116 additions & 479 deletions

File tree

app/views/components/widget/_fba.js.erb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function FBAform(d, N) {
2727
},
2828
init: function(options) {
2929
this.javascriptIsEnabled();
30+
this.formInitiatedAt = Date.now();
3031
this.options = options;
3132
if (this.options.loadCSS) {
3233
this._loadCss();
@@ -210,7 +211,16 @@ function FBAform(d, N) {
210211
var formElement = this.formElement();
211212
var self = this;
212213
if (self.validateForm(formElement)) {
213-
// disable submit button and show sending feedback message
214+
<%- if form.enable_turnstile? %>
215+
const elapsed = Date.now() - self.turnstileInitiatedAt;
216+
if (elapsed > 5 * 60 * 1000) {
217+
self.initTurnstile();
218+
self.turnstileInitiatedAt = Date.now();
219+
return false;
220+
}
221+
<% end %>
222+
223+
// disable submit button
214224
var submitButton = formElement.querySelector("[type='submit']");
215225
submitButton.disabled = true;
216226
submitButton.classList.add("aria-disabled");
@@ -716,14 +726,11 @@ function FBAform(d, N) {
716726
script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js";
717727
script.async = true;
718728
script.defer = true;
719-
script.onload = this.initTurnstile
729+
script.onload = this.initTurnstile;
730+
this.turnstileInitiatedAt = Date.now();
720731
if (!window.turnstile) {
721732
document.head.appendChild(script);
722733
}
723-
724-
setInterval(() => {
725-
window.turnstile.reset('#turnstile-container');
726-
}, 5 * 60 * 1000);
727734
},
728735
initTurnstile: function() {
729736
turnstile.remove("#turnstile-container");

0 commit comments

Comments
 (0)