Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto submit form after 5 seconds

I have a form that i am trying to submit after the page loads and 5 seconds has gone passed.. i have tried setTimeout but it doesnt appear to be working.. can anyone suggest why this would be the case, i have jQuery on the site but couldnt get delay() working either

<form action="" name="cartCheckout" id="cartCheckout" method="post"> 
<input type="hidden" name="action" value="checkout" />
<input type="hidden" name="save" value="1" />
<input type="hidden" name="orderID" value="<?php echo $GLOBALS['CHECKOUT_ORDERID']; ?>" />

<div class="itembox" id="step4box">
    <div id="progress">
        <ul>
        <li>Your Cart</li>
        <li>Your Details</li>
        <li class="current">Payment</li>
        <li>Confirmation</li>
        </ul>
    </div>
<div id="words" class="gothbold">Please wait while we we redirect you to<br />Paypal for a secure payment method.</div>

<a class="redirect" href="javascript:void(0)" title="Proceed to Paypal" onClick="document.cartCheckout.submit();"><span>If you aren't redirected in 5 seconds click here</span></a><br />

<a class="cancel" href="javascript:void(0)" title="Return to details" onClick="jQuery('#step4box').hide();jQuery('#step2box').show();"><span>Cancel</span></a>    
</div> 
<script type="text/javascript">
    window.onload=function(){ 
        window.setTimeout(document.cartCheckout.submit(), 5000);
    };
</script>
</form>     

any help would be greatly appreciated, thanks in advance

EDIT:

changed to this via a combination of @Alex, @user824294 and this question: How Can I create A 5 second Countdown timer with jquery that ends with a login popup?. Now working great and with great functionality of a countdown. Thanks guys!

window.onload=function(){ 
    var counter = 5;
    var interval = setInterval(function() {
        counter--;
        $("#seconds").text(counter);
        if (counter == 0) {
            redirect();
            clearInterval(interval);
        }
    }, 1000);

};

function redirect() {
    document.checkout_paypal.submit();
}
like image 775
haxxxton Avatar asked Jan 17 '12 00:01

haxxxton


2 Answers

Pass a reference to the function instead.

window.onload=function(){ 
    window.setTimeout(document.cartCheckout.submit.bind(document.cartCheckout), 5000);
};

...or...

window.onload=function(){ 
    window.setTimeout(function() { document.cartCheckout.submit(); }, 5000);
};

You are currently executing the function, and then passing its return value to setTimeout().

Also, if you wish to execute closer to exactly 5 seconds, you'd need to a setInterval() or recursive setTimeout() and examine +new Date.

like image 80
alex Avatar answered Sep 18 '22 14:09

alex


You can have it realy simple, try this guys :

<form name="xyz"...>
...
<script type="text/javascript">
<!--
   var wait=setTimeout("document.xyz.submit();",5000);
//-->
</script>
</form>
like image 26
mario Avatar answered Sep 19 '22 14:09

mario