Javascript 5 秒后自动提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8888166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto submit form after 5 seconds
提问by haxxxton
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
我有一个表单,我试图在页面加载并且 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>
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!
通过@Alex、@user824294 和这个问题的组合更改为:How Can I create A 5seconds Countdown timer with jquery that end withan 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();
}
采纳答案by alex
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()
.
您当前正在执行该函数,然后将其返回值传递给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
.
此外,如果您希望执行更接近 5 秒,则需要 asetInterval()
或递归setTimeout()
并检查+new Date
.
回答by mario
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>
回答by auroranil
Try doing this instead:
尝试这样做:
window.onload=function(){
window.setTimeout("redirect()", 5000);
// OR
window.setTimeout(function() { redirect(); }, 5000);
};
function redirect() {
document.cartCheckout.submit();
}