How to submit a form in Javascript with a delay

I am trying to submit a form automatically with a delay in the chrome extension that I am writing and it does not seem to be submitted. Below is my form and javascript:

function submitForm() { // submits form document.getElementById("ismForm").submit(); } if (document.getElementById("ismForm")) { setTimeout("submitForm()", 5000); // set timout } <form method="post" id="ismForm" name="ismForm" action="http://www.test.com" class=""> <label for="searchBox">Search </label> <input type="text" id="searchBox" name="q" value=""> <input type="hidden" id="sayTminLength" value="3"><input type="hidden" id="coDomain" value="US"><input class="button" type="submit" id="search.x" name="search.x" value="Search" autocomplete="off"> </form> 
+4
source share
5 answers

I don’t know the context, but it may be that the page has not yet been fully loaded - you can try to put

 if (document.getElementById("ismForm")) { setTimeout("submitForm()", 5000); // set timout } 

in body onLoad (). As one more thing, try putting both a simple warning in front of setTimeout and at the beginning of submitForm () to confirm that the timeout is triggered first.

+2
source

Here is what you need to do (copy and paste):

 <html> <head> <script type="text/javascript"> function submitForm() { // submits form document.getElementById("ismForm").submit(); } function btnSearchClick() { if (document.getElementById("ismForm")) { setTimeout("submitForm()", 5000); // set timout } } </script> </head> <body> <form method="post" id="ismForm" name="ismForm" action="http://www.test.com" class=""> <label for="searchBox">Search </label> <input type="text" id="searchBox" name="q" value=""> <input type="hidden" id="sayTminLength" value="3"> <input type="hidden" id="coDomain" value="US"> <input class="button" onclick="btnSearchClick();" type="button" id="search.x" name="search.x" value="Search" autocomplete="off"> </form> </body> </html> 

Or, if you want to submit the form in 5 seconds, attach the btnSearchClick () call to the windown.onload event as follows: window.onload = btnSearchClick

+1
source

Try the following:

 <form method="post" action="yourpage/" id="customForm"> <input type="text" name="input1"/> <input type="text" name="input2"/> </form> <button id="submit">SubmitForm</button><!-- Outside of form --> <script> function submitForm() { document.getElementById("customForm").submit() } document.getElementById('submit').onclick = function() { setTimeout(submitForm, 3000); } </script> 
+1
source

Here is another simple solution:

 <script type="text/javascript"> function formSubmit(){ document.getElementById("ismForm").submit(); } window.onload=function(){ window.setTimeout(formSubmit, 5000); }; </script> 
0
source

Here is a method that works for me:

  const form = $("#formId"); form.submit(() => { //some other functions you need to proceed before submit setTimeout(() => {}, 1200); return true; }); 

Now it will wait 1200 ms before submitting the form. Also, if you return false instead of true, the form will not be submitted.

0
source

All Articles