How do I submit a form in Javascript with a delay

Don't know the context, but it might be that the page has not been loaded yet completely - you might try putting

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

in body onLoad() event. As another thing, try putting as simple alert before setTimeout and at the start of submitForm() to confirm the timeout is getting fired in the first place.


Try this:

<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>

Here's 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 after 5 seconds, attach to the windown.onload event the call to btnSearchClick() like so: window.onload=btnSearchClick