Skip to content

Instantly share code, notes, and snippets.

@ozzi-
Last active May 21, 2021 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozzi-/f58c3273b53f70a1855f1b1145f7b323 to your computer and use it in GitHub Desktop.
Save ozzi-/f58c3273b53f70a1855f1b1145f7b323 to your computer and use it in GitHub Desktop.
JavaScript form submit timeout handling
<html>
Form being submitted
<!-- longresponse.phg will take 30 seconds to respons, in order to simulate a timeout -->
<form id="response" method="GET" action="https://oz-web.com/longresponse.php">
<input type="hidden" name="status" id="status" value="*empty*" />
</form>
<script>
const form = document.getElementById("response");
function handleFail(){
// replace this with whatever code you have to handle the timeout
location.href="https://github.com";
}
// we will wait for the form response for 3 seconds, otherwise we will abort
form.addEventListener('submit', (event) => {
setTimeout(handleFail, 3000);
})
// we can't use form.submit() as this wont trigger submit listener
form.dispatchEvent(new Event('submit'));
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment