如何检测表单提交超时并重新提交

How to detect form submit timeout and re-submit?

本文关键字:新提交 提交 表单提交 何检测 检测 超时      更新时间:2023-09-26

我们使用一个不可靠的端点来提交表单,偶尔会超时。

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

检测超时并尝试重新提交表单的最佳方法是什么?也许在放弃之前尝试5次。

这是一个第三方集成——表单加载到iframe中并自动提交以获取HTML内容。

注意:是的,一个更好的解决方案是修复超时。但我们需要拿出系统直播,不能等待第三方。

我认为最好的方法是在几秒钟内制作一个计数器,当计数器达到300(5分钟)时,再次提交表单。如果用户离开页面,则可以使用onbeforeunload停止计数器。

像这样的

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);
}

这是一个概念,这个代码没有经过测试。