Victor L
Victor L

Reputation: 10230

How to detect form submit timeout and re-submit?

We're using an unreliable endpoint to submit a form, which occasionally times out.

window.onload = function(){
  document.forms['form-to-third-party-endpoint'].submit()
}

What's the best way to detect the timeout and try re-submitting the form? Perhaps try it 5 times before giving up.

This is a 3rd-party integration -- the form loads up in an iframe and automatically submits to get the HTML content.

Note: Yes, a better solution would be to fix the timeout. But we need to take out system live and can't wait for the 3rd party.

Upvotes: 0

Views: 1519

Answers (1)

Marcos Pérez Gude
Marcos Pérez Gude

Reputation: 22158

I think the best way is to make a counter in seconds, and when it achieve to 300 (5 minutes) submit the form again. If user leaves page the counter can be stopped with a onbeforeunload.

Something like this

var counter = 0, interval;

window.onload = function() {
   interval = setInterval(addCounter, 1000); 
}

function addCounter() {
   counter++;
   if(counter === 300) {
     counter = 0;
     document.forms['form-to-third-party-endpoint'].submit()
   }
}

window.onbeforeunload = function() {
  clearInterval(interval);
}

That's a concept, this code was not tested.

Upvotes: 1

Related Questions