javascript – 5秒钟后自动提交表单

前端之家收集整理的这篇文章主要介绍了javascript – 5秒钟后自动提交表单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个表单,我试图提交后,页面加载和5秒已经通过..我已经尝试了setTimeout,但它似乎不工作..任何人都可以建议为什么会这样,我有jQuery在站点,但couldnt得到delay()工作
<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();
}

解决方法

传递对该函数的引用.
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()并检查新的Date.

原文链接:https://www.f2er.com/js/155413.html

猜你在找的JavaScript相关文章