5秒后自动提交表格

hax*_*ton 15 javascript forms settimeout

我有一个表单,我试图提交页面加载后5秒已经过去了..我已经尝试了setTimeout但它似乎没有工作..任何人都可以建议为什么会这样,我有jQuery上网站,但无法得到延迟()工作

<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>     
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,谢谢你提前

编辑:

通过@Alex,@ user824294和这个问题的组合改变了这个问题:如何使用以登录弹出结束的jquery创建一个5秒倒计时器?.现在工作得很好,并具有倒计时的强大功能.多谢你们!

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();
}
Run Code Online (Sandbox Code Playgroud)

ale*_*lex 14

传递对函数的引用.

window.onload=function(){ 
    window.setTimeout(document.cartCheckout.submit.bind(document.cartCheckout), 5000);
};
Run Code Online (Sandbox Code Playgroud)

...要么...

window.onload=function(){ 
    window.setTimeout(function() { document.cartCheckout.submit(); }, 5000);
};
Run Code Online (Sandbox Code Playgroud)

您当前正在执行该函数,然后将其返回值传递给setTimeout().

此外,如果您希望执行更接近5秒,您需要一个setInterval()或递归setTimeout()并检查+new Date.


小智 6

你可以让它真的很简单,试试这个家伙:

<form name="xyz"...>
...
<script type="text/javascript">
<!--
   var wait=setTimeout("document.xyz.submit();",5000);
//-->
</script>
</form>
Run Code Online (Sandbox Code Playgroud)