Last active
July 22, 2024 18:36
-
-
Save ozzi-/f58c3273b53f70a1855f1b1145f7b323 to your computer and use it in GitHub Desktop.
JavaScript form submit timeout handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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