5秒后自动提交表格

时间:2012-01-17 00:23:08

标签: 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>     

任何帮助都将非常感谢,提前感谢

编辑:

通过@Alex,@ user824294和此问题的组合更改为此:How Can I create A 5 second Countdown timer with jquery that ends with a login popup?。现在工作得很好,并具有倒计时的强大功能。谢谢你们!

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();
}

3 个答案:

答案 0 :(得分:12)

改为传递对该函数的引用。

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

...或...

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

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

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

答案 1 :(得分:5)

你可以把它变得非常简单,试试这个人:

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

答案 2 :(得分:1)

尝试这样做:

window.onload=function(){ 
    window.setTimeout("redirect()", 5000);
    // OR
    window.setTimeout(function() { redirect(); }, 5000);
};

function redirect() {
    document.cartCheckout.submit();
}