我有一个按钮可以触发 (1) 优惠券代码的应用和 (2) 提交整个付款表格。
问题是优惠券代码需要几秒钟的时间才能申请,所以在提交整个表格进行付款后才申请。如何延迟点击第二个付款按钮,以便首先应用优惠券代码按钮?
<script type="text/javascript">
jQuery('.js-coll-button2').click(function(e) {
e.preventDefault();
jQuery('#sc_checkout_form_1').find('.sc-coup-apply-btn').click();
jQuery('#sc_checkout_form_1').find('.sc-payment-btn').click();
});
</script>
最佳答案
那是不明智的。你不能保证一定的时间总是足够的。这取决于客户端的计算机和连接,如果第一个操作返回错误怎么办?
执行此操作的最佳方法是仅在第一个函数完成时才触发第二个函数。
编辑:我从未在 Javascript 中使用过 Stripe,但 API 手册清楚地说明每个 REST 调用都会返回一个代码。
而且,您不应该尝试强制点击,而应该使用函数。将您的功能与按钮分离!
<script type="text/javascript">
function doCoupon() {
return $.ajax(... do your stripe call ...);
}
function doAnotherThing() {
return $.ahax(... call your other thingie... );
}
function handleError() {
// ...
}
jQuery("#sc_checkout_form_1 .sc-coup-apply-btn").click(doCoupon);
jQuery("#sc_checkout_form_1 .sc-payment-btn").click(doAnotherThing);
jQuery('.js-coll-button2').click(function(e) {
e.preventDefault();
doCoupon().success(doAnotherThing).error(handleError);
});
</script>
关于javascript - jQuery 点击事件延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32006965/